How can I remove leading and trailing zeroes from numbers with sed/awk/perl?
Asked Answered
T

7

6

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.

Tetramethyldiarsine answered 10/9, 2013 at 9:0 Comment(0)
C
7

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
Charters answered 10/9, 2013 at 9:8 Comment(2)
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' numbersToots
You are right, updated. (I prefer two expressions for the sake of clarity)Charters
C
8
perl -pe 's/ ^0+ | 0+$ //xg' numbers
Carvey answered 10/9, 2013 at 9:20 Comment(0)
C
7

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
Charters answered 10/9, 2013 at 9:8 Comment(2)
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' numbersToots
You are right, updated. (I prefer two expressions for the sake of clarity)Charters
P
7

try this Perl:

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

  print $_;
 }
}
Potentiometer answered 10/9, 2013 at 9:9 Comment(0)
S
3

This might work for you (GNU sed):

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

or:

 sed -r 's/^0+|0+$//g' file
Shantung answered 10/9, 2013 at 10:8 Comment(0)
C
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 -----------------

Cannabis answered 25/6, 2019 at 16:43 Comment(0)
L
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'
Lecia answered 14/8, 2023 at 23:13 Comment(0)
R
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
Rheinland answered 16/8, 2023 at 3:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.