shantanuo
shantanuo

Reputation: 32336

remove leading characters from 12 digit numbers

I have a text file with id and number. All numbers are expected to be 10 digits long. In the following example only the 3rd row is correct.

36000003326|917668001520
36000003359|919005119822
36000003417|9153914209
36000003508|919454102627

If the number is 12 digits long, the first 2 country digits should be removed. The expected results are as follows:

36000003326|7668001520
36000003359|9005119822
36000003417|9153914209
36000003508|9454102627

The following sed is working but it does not check if the target entry is 10 digits or 12 digits long.

sed 's/|91/|/'

Upvotes: 0

Views: 224

Answers (3)

potong
potong

Reputation: 58430

This migh work for you (GNU sed):

sed -r 's/[0-9]{2}([0-9]{10})$/\1/' file

Upvotes: 0

curious_cat
curious_cat

Reputation: 837

How about this?

sed -r 's/\|..(.{10})/|\1/'

Upvotes: 3

Guru
Guru

Reputation: 16994

One way using awk:

awk -F"|" 'length($2)>10{$2=substr($2,3);}1' OFS="|" file

Upvotes: 1

Related Questions