5

I have file like this:

pup@pup:~/perl_test$ cat numbers 
1234567891
2133123131
4324234243
4356257472
3465645768000
3424242423
3543676586
3564578765
6585645646000
0001212122
1212121122
0003232322

In the above file I want to remove the leading and trailing zeroes so the output will be like this

pup@pup:~/perl_test$ cat numbers 
1234567891
2133123131
4324234243
4356257472
3465645768
3424242423
3543676586
3564578765
6585645646
1212122
1212121122
3232322

How to achieve this? I tried sed to remove those zeroes. It was easy to remove the trailing zeroes but not the leading zeroes.

Help me.

Agostino
  • 2,723
  • 9
  • 48
  • 65
EngineSense
  • 3,266
  • 8
  • 28
  • 44

7 Answers7

8
perl -pe 's/ ^0+ | 0+$ //xg' numbers
Ωmega
  • 42,614
  • 34
  • 134
  • 203
mpapec
  • 50,217
  • 8
  • 67
  • 127
7

try this Perl:

while (<>) {     
  $_ =~ s/(^0+|0+$)//g;

  print $_;
 }
}
rams0610
  • 1,041
  • 10
  • 8
6

sed looking for all zeros in the beginning of the line + looking for all zeros in the end:

$ sed -e 's/^0+//' -e 's/0+$//' numbers
1234567891
2133123131
4324234243
4356257472
3465645768
3424242423
3543676586
3564578765
6585645646
1212122
1212121122
3232322
Ωmega
  • 42,614
  • 34
  • 134
  • 203
fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • You should optimize your regex pattern(s). Why you use `[0]*` instead of `0+`? First, there is no need to use `[...]`, and more importantly you want one or more instances, so use `+` and not `*`. Furthermore, you may combine both patterns into one: `sed -r 's/^0+|0+$//g' numbers` – Ωmega Aug 16 '23 at 16:33
  • You are right, updated. (I prefer two expressions for the sake of clarity) – fedorqui Aug 17 '23 at 12:33
3

This might work for you (GNU sed):

sed 's/^00*\|00*$//g' file

or:

 sed -r 's/^0+|0+$//g' file
potong
  • 55,640
  • 6
  • 51
  • 83
0

Bash example to remove trailing zeros


    # ----------------- bash to remove trailing zeros ------------------
    # decimal insignificant zeros may be removed
    # bash basic, without any new commands eg. awk, sed, head, tail
    # check other topics to remove trailing zeros
    # may be modified to remove leading zeros as well

    #unset temp1

    if [ $temp != 0 ]           ;# zero remainders to stay as a float
    then

    for i in {1..6}; do             # modify precision in both for loops

    j=${temp: $((-0-$i)):1}         ;# find trailing zeros

    if [ $j != 0 ]              ;# remove trailing zeros
    then
        temp1=$temp1"$j"
    fi
        done
    else 
        temp1=0
    fi

    temp1=$(echo $temp1 | rev)
    echo $result$temp1

    # ----------------- END CODE -----------------

Zimba
  • 2,854
  • 18
  • 26
0

Expanding on the answer by fedorqui, this one-liner will

  • strip leading zeroes
  • strip trailing zeroes
  • strip a trailing decimal point if one exists and all numerals following the decimal are 0
    • e.g. 1.00 -> 1 instead of 1.00 -> 1.
sed -e 's/^[0]*//' -e 's/[0]*$//' -e 's/\.$//g'
JCS
  • 3
  • 4
0
gawk ++NF FS='^0+|0+$' OFS=
mawk 'gsub("^0*|0*$",_)'   # using [*] instead of [+] here
                           # ensures all rows print

1234567891
2133123131
4324234243
4356257472
3465645768
3424242423
3543676586
3564578765
6585645646
1212122
1212121122
3232322
RARE Kpop Manifesto
  • 2,453
  • 3
  • 11