Replacing a pattern in different cases in different columns with a single pattern


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing a pattern in different cases in different columns with a single pattern
# 1  
Old 05-15-2012
Replacing a pattern in different cases in different columns with a single pattern

Hi All

I am having pipe seperated inputs like

Code:
Adam|PeteR|Josh|PEter
Nick|Rave|Simon|Paul
Steve|smith|PETER|Josh
Andrew|Daniel|StAlin|peter
Rick|PETer|ADam|RAVE

i want to repleace all the occurrence of peter (in any case pattern PeteR,PEter,PETER,peter,PETer) with Peter so that output looks like

Code:
Adam|Peter|Josh|Peter
Nick|Rave|Simon|Paul
Steve|smith|Peter|Josh
Andrew|Daniel|StAlin|Peter
Rick|Peter|ADam|RAVE

Please note that this is a part of the input and there are more cases of peter in the pipe separated file (like PETEr, PeTeR,petER etc) all needs to be replaced with Peter.

Thanks in advance.

Sudeep

Last edited by Scrutinizer; 05-15-2012 at 04:01 PM.. Reason: Changed quote tags to code tags
# 2  
Old 05-15-2012
Hi sudeep.id,

One way using perl:
Code:
$ cat infile
Adam|PeteR|Josh|PEter
Nick|Rave|Simon|Paul
Steve|smith|PETER|Josh
Andrew|Daniel|StAlin|peter
Rick|PETer|ADam|RAVE
$ perl -pe 's/(?i)\b(peter)\b/ucfirst lc $1/ge' infile
Adam|Peter|Josh|Peter
Nick|Rave|Simon|Paul
Steve|smith|Peter|Josh
Andrew|Daniel|StAlin|Peter
Rick|Peter|ADam|RAVE

# 3  
Old 05-15-2012
Thanks

But the requirement has to be sufficed through a shell script or unix commands only. Can this be achieved via unix?

Quote:
Originally Posted by birei
Hi sudeep.id,

One way using perl:
Code:
$ cat infile
Adam|PeteR|Josh|PEter
Nick|Rave|Simon|Paul
Steve|smith|PETER|Josh
Andrew|Daniel|StAlin|peter
Rick|PETer|ADam|RAVE
$ perl -pe 's/(?i)\b(peter)\b/ucfirst lc $1/ge' infile
Adam|Peter|Josh|Peter
Nick|Rave|Simon|Paul
Steve|smith|Peter|Josh
Andrew|Daniel|StAlin|Peter
Rick|Peter|ADam|RAVE

# 4  
Old 05-15-2012
Code:
sed 's/[pP][eE][tT][eE][rR]/Peter/g' input > output

This User Gave Thanks to Corona688 For This Post:
# 5  
Old 05-15-2012
Ohh... I missed this solution... How foolish of me... Thanks a lot Smilie

Quote:
Originally Posted by Corona688
Code:
sed 's/[pP][eE][tT][eE][rR]/Peter/g' input > output

# 6  
Old 05-16-2012
try this

Code:
awk 'BEGIN{IGNORECASE=1}/peter/{gsub(/peter/,"Peter");print}' filename

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed -- Find pattern -- print remainder -- plus lines up to pattern -- Minus pattern

The intended result should be : PDF converters 'empty line' gpdftext and pdftotext?xml version="1.0"?> xml:space="preserve"><note-content version="0.1" xmlns:/tomboy/link" xmlns:size="http://beatniksoftware.com/tomboy/size">PDF converters gpdftext and pdftotext</note-content>... (9 Replies)
Discussion started by: Klasform
9 Replies

2. Shell Programming and Scripting

Pattern replacing

Hi, I have a text file with lots of text (strings,numbers,special characters etc). I am trying to replace any occurrence of these strings : 90% 91% 92% .... 100% I want to replace them with : "90%" "91%" "92%" .... "100%" I am now using 10 sed commands for replacement but I... (12 Replies)
Discussion started by: ctrld
12 Replies

3. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

4. UNIX for Dummies Questions & Answers

Match Pattern after certain pattern and Print words next to Pattern

Hi experts , im new to Unix,AWK ,and im just not able to get this right. I need to match for some patterns if it matches I need to print the next few words to it.. I have only three such conditions to match… But I need to print only those words that comes after satisfying the first condition..... (2 Replies)
Discussion started by: 100bees
2 Replies

5. Shell Programming and Scripting

replacing pattern

hi, I want to do replacing of some pattern by using sed. pattern : " white space / to white space / please help -bhrat (3 Replies)
Discussion started by: bhrat kapoor
3 Replies

6. Shell Programming and Scripting

Replacing pattern

Hi, I have a file which contains the below data. I want to search for a pattern server="http://bushby.mis.amat.com:12440" and remove it from the file. Please let me know how can i do this. <Object name="reverse-proxy-/endeavour/"> ObjectType fn="http-client-config" timeout="1800"... (6 Replies)
Discussion started by: Krrishv
6 Replies

7. Shell Programming and Scripting

Replacing pattern

Hi, I have a file which contains the below data. I want to change pattern to correct format. # tail -1 test.log | awk '{print $8}' 10/09/23 # I want the format to be 23/09/10 (5 Replies)
Discussion started by: nareshkumar522
5 Replies

8. Shell Programming and Scripting

search a pattern and if pattern found insert new pattern at the begining

I am trying to do some thing like this .. In a file , if pattern found insert new pattern at the begining of the line containing the pattern. example: in a file I have this. gtrow0unit1/gctunit_crrownorth_stage5_outnet_feedthru_pin if i find feedthru_pin want to insert !! at the... (7 Replies)
Discussion started by: pitagi
7 Replies

9. UNIX for Dummies Questions & Answers

Replacing between pattern

Hi all, I been searching the forum for a few hours now and can't find exactly what I need to replace text between two patterns. Below is what I want to accomplish /* Any text between these to sympols */ changed to.... /* This will be the new text to change */ ..and I'm... (3 Replies)
Discussion started by: Pauky
3 Replies

10. Shell Programming and Scripting

Replacing more than 1 pattern in a line

Hi, pls advise how we could make change to more than one pattern in a single line using sed.. ie: IGNORE abcd delete}}} COND IGNORE efgh}}} will yield in abcd delete}} COND IGNORE efgh}}} Pls note that IGNORE may not be in the start of line, IGNORE is intended to be... (6 Replies)
Discussion started by: Manan
6 Replies
Login or Register to Ask a Question