Replacing string length based on pattern


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Replacing string length based on pattern
# 8  
Old 12-22-2016
Remove leading digit examples:

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

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

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

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

This User Gave Thanks to Aia 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