linux shell批处理去除指定字符前所有的0

批处理去除指定字符前所有的0
题目
有文本 1.txt

0000acb0h 
0b0c00a000s 
0000h00ga00 
a0000000

要求
通过批处理将文本所有字符串字符a前的0去除输出如下

acb0h 
bca000s 
hga00 
a0000000

编写代码
#! /bin/bash

file=./1.txt
before=a
replace=0

while read line
do
        len=${#line}

        i=0
        str=            #保留指定字符前面字符

        while [ ${i} -lt ${len} ]
        do
                c=${line:${i}:1}

                #判断是否在指定字符之前
                if [ "${c}" == "${before}" ]
                then 
                        break
                fi

                #是否为需要删除的字符
                if [ "${c}" != "${replace}" ]
                then
                        str=${str}${c}
                fi

                let i++
        done

        # 保留指定字符后面的内容
        suffix=${line:${i}}

        echo ${str}${suffix}


done < ${file}

运行结果

--------------------- 
作者:dengjili 
来源:CSDN 
原文:https://blog.csdn.net/dengjili/article/details/78073585 
版权声明:本文为博主原创文章,转载请附上博文链接!

批处理去除指定字符前所有的0
题目
有文本 1.txt

0000acb0h 
0b0c00a000s 
0000h00ga00 
a0000000

要求
通过批处理将文本所有字符串字符a前的0去除输出如下

acb0h 
bca000s 
hga00 
a0000000

编写代码
#! /bin/bash

file=./1.txt
before=a
replace=0

while read line
do
        len=${#line}

        i=0
        str=            #保留指定字符前面字符

        while [ ${i} -lt ${len} ]
        do
                c=${line:${i}:1}

                #判断是否在指定字符之前
                if [ "${c}" == "${before}" ]
                then 
                        break
                fi

                #是否为需要删除的字符
                if [ "${c}" != "${replace}" ]
                then
                        str=${str}${c}
                fi

                let i++
        done

        # 保留指定字符后面的内容
        suffix=${line:${i}}

        echo ${str}${suffix}


done < ${file}

运行结果

--------------------- 
作者:dengjili 
来源:CSDN 
原文:https://blog.csdn.net/dengjili/article/details/78073585 
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自blog.csdn.net/qq_19004627/article/details/85197248
今日推荐