Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

sed

awk

perl

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.

like image 291
EngineSense Avatar asked Nov 29 '22 01:11

EngineSense


1 Answers

perl -pe 's/^0+ | 0+$//xg' numbers
like image 91
mpapec Avatar answered Dec 04 '22 04:12

mpapec