Loading...

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

Answer #1 100 %

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]*$//g' numbers
1234567891
2133123131
4324234243
4356257472
3465645768
3424242423
3543676586
3564578765
6585645646
1212122
1212121122
3232322
Answer #2 100 %
perl -pe 's/^0+ | 0+$//xg' numbers
Answer #3 100 %

try this Perl:

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

  print $_;
 }
}

You’ll also like:


© 2022 CodeForDev.com -