Replacing string length based on pattern

Hi All,

I have a file which is like below. I need to read all the patterns that starts with P and then replace the 9 digit values to 8 digit values (remove leading integer). Can you please help

Example : ( Please look below File)

File :

P,1
M1,
P195925856,P195925857,P195925858,P195926007,P195926008,P195926009,P195926010,P195926011,
P195926012,P195926013,P195926014,P195926015,P195926016,P195926017,P371622859,P371622860,
P371622861,P195926018,P371622862,P371622863,P371622864,P195926019,P195926020,P195926021,
P195926022,P195926023,P195926024,P195926025,P195926026,P195926027,P195926029,P371622865,
P371622866,P371622867,P195926030,P195926031,P371622868,P371622869,P371622870,P195926032,
P195926033,P195926034,P195926035,P195926036,P195926037,P195926038,P195926039,P195926040,
P195926041,P371622871,P371622872,P195926042,P195926043,P195926044,P195926045,P195926047,
M,1,1,0
M,1,2,0
M1,3,0

Hello arunkumar_mca,

Could you please try following and let me know if this helps you.

awk '{gsub("P195925856","P95925856",$0);gsub("P195925857","P95925857",$0);print}'  Input_file

Above will change all occurrences of these digits, so if you want in each line only 1 occurrence then you should change gsub to sub in above code.

Thanks,
R. Singh

1 Like

Thanks for your reply.

I need to change all the P dataset.Not the 2 alone. Is there a wild card pattern where I can use to replace all the dataset with P

P195925856,P195925857,P195925858,P195926007,P195926008,P195926009,P195926010,P195926011,
P195926012,P195926013,P195926014,P195926015,P195926016,P195926017,P371622859,P371622860,
P371622861,P195926018,P371622862,P371622863,P371622864,P195926019,P195926020,P195926021,
P195926022,P195926023,P195926024,P195926025,P195926026,P195926027,P195926029,P371622865,
P371622866,P371622867,P195926030,P195926031,P371622868,P371622869,P371622870,P195926032,
P195926033,P195926034,P195926035,P195926036,P195926037,P195926038,P195926039,P195926040,
P195926041,P371622871,P371622872,P195926042,P195926043,P195926044,P195926045,P195926047,

Try:

sed 's/P[0-9]\([0-9]\{8\},\)/P\1/g' file
1 Like

awk -F, '{for(i=1;i<=NF;i++) if ($i~/^P/) $i="P" substr($i,3)}1' OFS=, myFile

1 Like

Thanks a lot.

In case if I need to remove leading digit what should I do.

Example

Thanks
Arun

Hello arunkumar_mca,

Taking adaption from Scrutinizer's code here a bit change into it will do the trick here.

sed 's/\(P[0-9]\{8\}\)[0-9],/\1,/g'  Input_file

Thanks,
R. Singh

1 Like

Remove leading digit examples:

perl -pe 's/P\d/P/g' arunkumar.file
P,1
M1,
P95925856,P95925857,P95925858,P95926007,P95926008,P95926009,P95926010,P95926011,
P95926012,P95926013,P95926014,P95926015,P95926016,P95926017,P71622859,P71622860,
P71622861,P95926018,P71622862,P71622863,P71622864,P95926019,P95926020,P95926021,
P95926022,P95926023,P95926024,P95926025,P95926026,P95926027,P95926029,P71622865,
P71622866,P71622867,P95926030,P95926031,P71622868,P71622869,P71622870,P95926032,
P95926033,P95926034,P95926035,P95926036,P95926037,P95926038,P95926039,P95926040,
P95926041,P71622871,P71622872,P95926042,P95926043,P95926044,P95926045,P95926047,
M,1,1,0
M,1,2,0
M1,3,0
perl -pe 's/P\d(\d+,)/P$1/g' arunkumar.file
P,1
M1,
P95925856,P95925857,P95925858,P95926007,P95926008,P95926009,P95926010,P95926011,
P95926012,P95926013,P95926014,P95926015,P95926016,P95926017,P71622859,P71622860,
P71622861,P95926018,P71622862,P71622863,P71622864,P95926019,P95926020,P95926021,
P95926022,P95926023,P95926024,P95926025,P95926026,P95926027,P95926029,P71622865,
P71622866,P71622867,P95926030,P95926031,P71622868,P71622869,P71622870,P95926032,
P95926033,P95926034,P95926035,P95926036,P95926037,P95926038,P95926039,P95926040,
P95926041,P71622871,P71622872,P95926042,P95926043,P95926044,P95926045,P95926047,
M,1,1,0
M,1,2,0
M1,3,0

Remove trailing digit examples:

perl -pe 's/(P\d+)\d,/$1,/g' arunkumar.file
P,1
M1,
P19592585,P19592585,P19592585,P19592600,P19592600,P19592600,P19592601,P19592601,
P19592601,P19592601,P19592601,P19592601,P19592601,P19592601,P37162285,P37162286,
P37162286,P19592601,P37162286,P37162286,P37162286,P19592601,P19592602,P19592602,
P19592602,P19592602,P19592602,P19592602,P19592602,P19592602,P19592602,P37162286,
P37162286,P37162286,P19592603,P19592603,P37162286,P37162286,P37162287,P19592603,
P19592603,P19592603,P19592603,P19592603,P19592603,P19592603,P19592603,P19592604,
P19592604,P37162287,P37162287,P19592604,P19592604,P19592604,P19592604,P19592604,
M,1,1,0
M,1,2,0
M1,3,0
perl -pe 's/(?<=P\d{8})\d//g' arunkumar.file
P,1
M1,
P19592585,P19592585,P19592585,P19592600,P19592600,P19592600,P19592601,P19592601,
P19592601,P19592601,P19592601,P19592601,P19592601,P19592601,P37162285,P37162286,
P37162286,P19592601,P37162286,P37162286,P37162286,P19592601,P19592602,P19592602,
P19592602,P19592602,P19592602,P19592602,P19592602,P19592602,P19592602,P37162286,
P37162286,P37162286,P19592603,P19592603,P37162286,P37162286,P37162287,P19592603,
P19592603,P19592603,P19592603,P19592603,P19592603,P19592603,P19592603,P19592604,
P19592604,P37162287,P37162287,P19592604,P19592604,P19592604,P19592604,P19592604,
M,1,1,0
M,1,2,0
M1,3,0
1 Like