Replacing string length based on pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Replacing string length based on pattern
# 1  
Old 12-21-2016
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)

Quote:
P195925856 needs to replaced as P95925856
P195925857 needs to be replaced as P95925857
and to all the rest on the file
File :

Code:
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


Last edited by Scrutinizer; 12-21-2016 at 04:24 PM..
# 2  
Old 12-21-2016
Hello arunkumar_mca,

Could you please try following and let me know if this helps you.
Code:
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
This User Gave Thanks to RavinderSingh13 For This Post:
# 3  
Old 12-21-2016
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

Code:
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,


Last edited by Scrutinizer; 12-21-2016 at 04:24 PM.. Reason: code tags
# 4  
Old 12-21-2016
Try:
Code:
sed 's/P[0-9]\([0-9]\{8\},\)/P\1/g' file

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 12-21-2016
awk -F, '{for(i=1;i<=NF;i++) if ($i~/^P/) $i="P" substr($i,3)}1' OFS=, myFile
This User Gave Thanks to vgersh99 For This Post:
# 6  
Old 12-21-2016
Thanks a lot.

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

Example

Quote:
P195925856 needs to replaced as P19592585
P195925857 needs to be replaced as P19592585
and to all the rest on the file

Thanks
Arun
# 7  
Old 12-21-2016
Hello arunkumar_mca,

Taking adaption from Scrutinizer's code here a bit change into it will do the trick here.
Code:
sed 's/\(P[0-9]\{8\}\)[0-9],/\1,/g'  Input_file

Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to find string based on pattern and search for its corresponding rows in column

Experts, Need your support for this awk script. we have only one input file, all these column 1 and column 2 are in same file and have to do lookup for values in one file(column1 and column2) but output we need in another file Need to grep row whose string contains 9K from column 1. When found... (6 Replies)
Discussion started by: as7951
6 Replies

2. Shell Programming and Scripting

awk to insert missing string based on pattern in file

Using the file below, which will always have the first indicated by the digit after the - and last id in it, indicated by the digit after the -, I am trying to use awk to print the missing line or lines in file following the pattern of the previous line. For example, in the file below the next... (4 Replies)
Discussion started by: cmccabe
4 Replies

3. Shell Programming and Scripting

Add string based on character length

Good day, I am a newbie here and thanks for accepting me I have a task to modify input data where my input data looks like 123|34567|CHINE 1|23|INDIA 34512|21|USA 104|901|INDIASee that my input has two columns with different character length but max length is 5 and minimum length is 0 which... (1 Reply)
Discussion started by: fastlearner
1 Replies

4. Shell Programming and Scripting

Sort a hash based on the string length of the values

Hi, I want to be able to sort/print a hash based on the string length of the values. For example %hash = ( key1 => 'jeri', key2 => 'corona', key3 => 'una, ); I want to be able to print in the following order (smallest to largest) una,jeri,corona OR... (1 Reply)
Discussion started by: jdilts
1 Replies

5. Shell Programming and Scripting

sed or awk command to replace a string pattern with another string based on position of this string

here is what i want to achieve... consider a file contains below contents. the file size is large about 60mb cat dump.sql INSERT INTO `table1` (`id`, `action`, `date`, `descrip`, `lastModified`) VALUES (1,'Change','2011-05-05 00:00:00','Account Updated','2012-02-10... (10 Replies)
Discussion started by: vivek d r
10 Replies

6. Shell Programming and Scripting

help - sed - insert space between string of form XxxAxxBcx, without replacing the pattern

If the string is of the pattern XxxXyzAbc... The expected out put from sed has to be Xxx Xyz Abc ... eg: if the string is QcfEfQfs, then the expected output is Qcf Ef Efs. If i try to substitute the pattern with space then the sed will replace the character or pattern with space,... (1 Reply)
Discussion started by: frozensmilz
1 Replies

7. Shell Programming and Scripting

Concatenating and appending string based on specific pattern match

Input #GEO-1-type-1-fwd-Initial 890 1519 OPKHIJEFVTEFVHIJEFVOPKHIJTOPKEFVHIJTEFVOPKOPKHIJHIJHIJTTOPKHIJHIJEFVEFVOPKHIJOPKHIJOPKEFVEFVOPKHIJHIJEFVHIJHIJEFVTHIJOPKOPKTEFVEFVEFVOPKHIJOPKOPKHIJTTEFVEFVTEFV #GEO-1-type-2-fwd-Terminal 1572 2030... (7 Replies)
Discussion started by: patrick87
7 Replies

8. Shell Programming and Scripting

Retrieve string from each line in a file based on a pattern in Unix

I have a file which contains several lines. Sample content of the file is as below. OK testmessage email<test@123> NOK receivemessage email<123@test> NOK receivemessage email(123@test123) NOK receivemessage email<abc@test> i would like to know by scripting will... (10 Replies)
Discussion started by: ramasar
10 Replies

9. Shell Programming and Scripting

Replacing one Char in a string of variable length

Hi all, I am trying to find the best way of making a change to 1 char in a string, the string can be between 1 and 14 characters. I am reading a line in from a file which contains 012341231231:2:102939283:NNN: Require :NBN: 012838238232:3:372932:NNN: Require :NNB: I need to change 1 N or a... (8 Replies)
Discussion started by: nkwilliams
8 Replies

10. UNIX for Dummies Questions & Answers

String spllittinf based on pattern

Hi, I have a sample.properties file and it contains the following content. today--RSS_URL=http://someurl i need to split the string and need the result in the folliwng form. x1=today x2=RSS_URL x3=http://someurl Your immediate response will be graetly appreciated (3 Replies)
Discussion started by: eswarreddya
3 Replies
Login or Register to Ask a Question