Sohan
Sohan

Reputation: 6819

How to strip off digits at beginning using sed?

I have pipe separated file. I want to strip off digits starting with '00' of sub id keeping rest of numbers same. For eg: Karen Calvert50335491|0020380050335491|ACTIVE|100|KINGSPORT|[email protected]|8353/3000|RESIDENTIAL|FiberNode|TENNESSEE|00:00:00:20:0f:03|EAST|423-343-9250|HSIPLUS|1826 HIGHLAND ST|Service1|MA|01602

expected o/p-

Karen Calvert50335491|20380050335491|ACTIVE|100|KINGSPORT|[email protected]|8353/3000|RESIDENTIAL|FiberNode|TENNESSEE|00:00:00:20:0f:03|EAST|423-343-9250|HSIPLUS|1826 HIGHLAND ST|Service1|MA|01602

Note:In this file there are some mac address also wich are satritng with '00'

Any help appreciated. Thanks

Upvotes: 1

Views: 122

Answers (3)

potong
potong

Reputation: 58578

This might work for you (GNU sed):

sed -r 's/^([^|]*\|)0+/\1/' file

or more generally:

sed -r 's/^(([^|]*\|){1})0+/\1/' file

where in this case 1 is the nth -1 field

Upvotes: 2

Ed Morton
Ed Morton

Reputation: 204628

Good grief. Try the sed solutions if you wanted to manipulate the 7th field instead of the second one. Just use awk:

awk 'BEGIN{FS=OFS="|"} {sub(/^0+/,"",$2);print}' file

If you wanted to change the 7th field instead of the 2nd, you'd just change $2 to $7.

Upvotes: 1

perreal
perreal

Reputation: 98118

Using sed:

 sed 's/^\([^|]*\)|0*\([^|]*\)/\1|\2/' input

Which matches, captures and preserves the first column in \1 via ^\([^|]*\) and captures the second column in \2, skipping the leading 0's via 0*\([^|]*\). Then it replaces the matched part (columns 1 and 2) with the captured parts of these columns via /\1|\2/.

Upvotes: 3

Related Questions