AWK find/replace 2 strings in one shot


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting AWK find/replace 2 strings in one shot
# 1  
Old 07-06-2009
AWK find/replace 2 strings in one shot

Friends,
I have a file with contents like:

interface Serial0/4/0/0/1/1/1/1:0
encapsulation mfr
multilink
group 101
interface Serial0/4/0/0/1/1/1/2:0
encapsulation ppp
multilink
group 101

I just have to repace mfr with ppp and ppp with mfr in a single shot.

I tried using this:
awk '{gsub(/mfr/,"ppp");{gsub(/ppp/,"mfr")}}; 1'

But this 1st replaces mfr with ppp and then ppp with mfr, so finally i get all mfr.

Can somebody help me with this? Can we use SED?
# 2  
Old 07-06-2009
Code:
awk '
     /mfr/ {gsub(/mfr/,"ppp"); print; next}
     /ppp/ {gsub(/ppp/,"mfr"); print; next}
     {print}
' infile

# or sed

sed '/ppp/ {s/ppp/mfr/g; n}; /mfr/ {s/mfr/ppp/g; n}' infile

# 3  
Old 07-06-2009
Thanks zaxxon, it works.
# 4  
Old 07-06-2009
Bug

sed 's/\(mfr\)/\1ttmmpp/g' a1 | sed 's/\(ppp\)/mfr/g' | sed 's/\(mfrttmmpp\)/ppp/g'

It may also work
# 5  
Old 07-06-2009
if there will be more than one fmr or ppp occured per line, maybe below perl one is a choosable solution.

Code:
my %hash=(good=>'bad',bad=>'good');
while(<DATA>){
	chomp;
	my @tmp=split;
	print join " ", map {$hash{$_}?$hash{$_}:$_} @tmp;
	print "\n";
}
__DATA__
good bad hello good bad
bad hi good bad good
good how are good bad you bad
bad bad good good ohho

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

sed find 2 strings and replace one

Hi Everyone, I want to find this 2 strings in a single line a file and replace the second string. this is the line i need to find <param name="user" value="CORE_BI"/> find user and CORE_BI and replace only CORE_BI with admin so finally the line should look like this. <param... (5 Replies)
Discussion started by: shajay12
5 Replies

2. Shell Programming and Scripting

sed Find and Replace Text Between Two Strings or Words

I am looking for a sed in which I can recognize all of the text in between two indicators and then replace it with a place holder. For instance, the 1st indicator is a list of words "no|noone|havent" and the 2nd indicator is a list of punctuation ".|,|!".From a sentence such as "noone... (3 Replies)
Discussion started by: owwow14
3 Replies

3. Programming

How to replace the complex strings from a file using sed or awk?

Dear All, I am having a requirement to find the difference between 2 files and generate a discrepancy report out of it as an html page. I prefer using diff -y file1 file2 since it gives user friendly layout to know any discrepancy in the record and unique records among the 2 file. Here's how it... (12 Replies)
Discussion started by: Badhrish
12 Replies

4. Shell Programming and Scripting

Using awk to replace strings

Hi.. I have a file that has the following content : abc 213 24 213 pqr 456#34 678 xyz 213 45%213 i need to write an awk script that will replace the second 213 in all the lines, if it is present. The IFS can not be specified and can be random. The number of lines in the file and the... (5 Replies)
Discussion started by: Hermione Grange
5 Replies

5. Shell Programming and Scripting

Find multiple strings and replace single string

Hi, following Perl code i used for finding multiple strings and replace with single string. code: #!/usr/bin/perl my @files = <*.txt>; foreach $fileName (@files) { print "$fileName\n"; my $searchStr = ',rdata\)' | ',,rdata\)' | ', ,rdata\)'; my $replaceStr =... (2 Replies)
Discussion started by: chettyravi
2 Replies

6. UNIX for Advanced & Expert Users

Find and replace txt between two strings in flat file

Hi There... I need to serach and replace strngs in a text file. My file has; books.amazon='Let me read' news.bestseller='xyz' expected output is books.amazon=NONFOUND news.bestseller=NONFOUND Can I first find the text between string1= books.amazon=' and string2= ' (locate the text... (1 Reply)
Discussion started by: Hiano
1 Replies

7. UNIX for Dummies Questions & Answers

how to find and replace strings in multiple files

Hi All, Iam new to unix, I need to find string and replace it in the file name. Like text_123_0.txt,text_123_1.txt,text_123_2.txt. I need to search 123 and replace it with 234 . Is there any unix command to replace them in single command since i have 5 directories. So i need to go each and every... (0 Replies)
Discussion started by: etldeveloper
0 Replies

8. Shell Programming and Scripting

Replace Strings with sed or awk

Hello i need some help with the usage of sed. Situation : 2 textfiles, file.in , file.out In the first textfile which is called file.in are the words for the substitution. Every word is in a new-line like : Firstsub Secondsub Thridsub ... In the second textflie wich is called file.out is... (5 Replies)
Discussion started by: Kingbruce
5 Replies

9. Shell Programming and Scripting

Help with Find/Replace Javascript Injected Strings in mulitple files

Hi, guys, I'm not a high-end programmer, but I've been trying to write a script to remove all of the b.rtbn2.cn (and b.adserv.cn and any future variation) injected script tags on the server. (Still working on security fixes to prevent it in the future, just need to clean up now.) My approach is... (1 Reply)
Discussion started by: zzlegs
1 Replies

10. Shell Programming and Scripting

awk find/replace

Greetings all. I have web site that has long option and switch lists. When I insert something new into these files, the lists need to be reordered. IE: 1 => apple 2 => pear 3 => bannana 4 => orange --------------------- Add grape as #2 1 => apple 2 => grape 3 => pear 4 =>... (2 Replies)
Discussion started by: RobertSubnet
2 Replies
Login or Register to Ask a Question