Exact Search and Replace using AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Exact Search and Replace using AWK
# 1  
Old 03-24-2011
Exact Search and Replace using AWK

Hello.
I have written the following script to search and replace from one file into another.
Code:
#awk script to search and replace from file a in file b
NR == FNR { A[$1]=$2; next }
  { for( a in A ) sub(a, A[a])}1 file2 file1

While the function works pretty well, I want
a. The word in File 2 to mantained and also the replacement to be provided.
b. If there is a residue i.e. a word is not found in the master file (SnR) file, it should be listed in a file called residue.
How can I do this. I am new to awk and have reached this far in awk but tips to handle the issue would help
Sample files:
File1 (SnR file)
Code:
john=mary
pup=dog
cat=kitten

File2 (File for replacement)
Code:
john
pup
cat
jumbo

OUTPUT
Code:
=mary
=dog
=kitten

Expected output:
Code:
john=mary
pup=dog
cat=kitten

The residue "jumbo" could either be flagged as residue with a convenient marker say ! or stored in a separate file.
Many thanks in advance.

Last edited by Franklin52; 03-24-2011 at 04:04 AM.. Reason: Please use code tags
# 2  
Old 03-24-2011
This should do it (With ! marker for residue)
Code:
awk 'NR==FNR{A[$1]=$2;next}{if($0 in A) print $0"="A[$0]; else print "!"$0}' FS="=" SnR replacement

Or this to put residue in file "residue"
Code:
awk 'NR==FNR{A[$1]=$2;next}{if($0 in A) print $0"="A[$0]; else print $0 >> "residue" }' FS="=" SnR replacement

This User Gave Thanks to Chubler_XL For This Post:
# 3  
Old 03-24-2011
Dear Chubler XL,
The solution works but partially.
While the search and replace works wonderfully, the residue does not work.
I am posting below the output of both solutions:
SOLUTION1 (WITH RESIDUE IN FILE)
Code:
!john=mary
john=mary
!pup=dog
pup=dog
!cat=kitten
cat=kitten
!
!

Obviously the residue and postive mappers are jumbled up.

The second solution you proposed (with residue in a separate file) gives correct mapping
Code:
john=mary
pup=dog
cat=kitten

But the residual file is a problem:
Code:
john=mary
pup=dog
cat=kitten

john=mary
pup=dog
cat=kitten

Many thanks for your help,

Last edited by Franklin52; 03-24-2011 at 04:05 AM.. Reason: Please use code tags
# 4  
Old 03-24-2011
Cant see how this could not work - going fine for me here.

Are your sure your version of file2 (replacement) dosn't look like this:

Code:
john=mary
john
pup=dog
pup
cat=kitten
cat

Also note: residual file is appended to each time so if you don't want to keep old (previous) residuals you should remove the file first.
# 5  
Old 03-24-2011
Sorry for the delay in answering, but we had a broadband outage. I had no such luck. I copied the two files and renamed them master and slave and I still continue to get the same outputs which I posted.
All I should have got was 3 replacements with "jumbo" as the residue.
I am really perplexed. Is it once more because I work in DOS (blame it on DOS).
Many thanks all the same for all your help.
# 6  
Old 03-25-2011
Perhaps it's the FS="=" try this:

Code:
awk -F= 'NR==FNR{A[$1]=$2;next}{if($0 in A) print $0"="A[$0]; else print "!"$0}' SnR replacement

This User Gave Thanks to Chubler_XL For This Post:
# 7  
Old 03-25-2011
Dear Chubler_XL
I tried the syntax you gave and ran it with all the versions I have of Awk but they all gave the same result:
In despair, I even tried reversing the order, here is what I get:

C:\Users\XP-HOME\Desktop>awk -f snr.gk master slave
!john
john
!pup
pup
!cat
cat
!jumbo
jumbo

C:\Users\XP-HOME\Desktop>awk -f snr.gk slave master
!john=mary
john=mary
!pup=dog
pup=dog
!cat=kitten
cat=kitten

The test files are the same as given in my request:
MASTER:
john=mary
pup=dog
cat=kitten
SLAVE:
john
pup
cat
jumbo
I have even as a last resort, while writing this sorted both master and slave, the output is consistently wrong.
I am perplexed, since the syntax is absolutely correct :
Open the two files.
If match found spew it out.
What does not match flag as residue and show on screen or alternatively (as was in the earlier script) store in file.
One of the mysteries of Awk which remains insoluble.
Many thanks once again for all your kind help
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search and replace with awk

Hi Shell Tigers, I am trying to acheive search and replace strings within a setup file. Help much appreciated. test.setup ORACLE_HOME=/oracle/product/11.0.0/home01 PATH1=/perm_loc/3222/FTP/cfg1 PATH2=/perm_loc/3222/FTP/cfg2/bin PATH3=/perm_loc/3222/FTP/cfg3/bin So... (3 Replies)
Discussion started by: jville
3 Replies

2. Shell Programming and Scripting

Awk: search and replace

I have a file which requires modification via a shell script. Need to do the following: 0. take input from user for new text. 1. search for a keyword in the file. 2. going forward, search for another keyword. 3. Replace this line with user supplied input. for e.g., my file has the following... (6 Replies)
Discussion started by: chingupt
6 Replies

3. Shell Programming and Scripting

QUESTION1: grep only exact string. QUESTION2: find and replace only exact value with sed

QUESTION1: How do you grep only an exact string. I am using Solaris10 and do not have any GNU products installed. Contents of car.txt CAR1_KEY0 CAR1_KEY1 CAR2_KEY0 CAR2_KEY1 CAR1_KEY10 CURRENT COMMAND LINE: WHERE VARIABLE CAR_NUMBER=1 AND KEY_NUMBER=1 grep... (1 Reply)
Discussion started by: thibodc
1 Replies

4. Shell Programming and Scripting

How to use SED or AWK to search and replace an exact string

I have a file DS1 DDS DS I want to replace only "DS" to "DSmail.blah.com" in a lot of files. I tried sed 's/DS/DSmail.blah.com' but it changes all the lines . thanks in advance (2 Replies)
Discussion started by: gubbu
2 Replies

5. Shell Programming and Scripting

search and replace exact string

Hello Everyone, Im trying to run a search and replace of exact strings and the strings that im using variables that are passed through an array in a while loop. Here is a snip of my code: USEROLD=`cat oldusers` USERNEW=`cat newusers` USEROLDARRAY=( $USEROLD ) USERNEWARRAY=( $USERNEW )... (4 Replies)
Discussion started by: skizim
4 Replies

6. Shell Programming and Scripting

awk search and replace field

I am writing a c++ program that has many calls of pow(input,2). I now realize that this is slowing down the program and these all should be input * input for greater speed. There should be a simple way of doing this replacement throughout my file with awk, but I am not very familiar with awk.... (2 Replies)
Discussion started by: bluejayek
2 Replies

7. Shell Programming and Scripting

search and replace with AWK

Suchen und Ersetzen mit AWK hello, i'm not good in scripting and asking for your help. With this script i change some text parts in diffent datafiles. It works without problems, but i need to get some informations about what changes in wich datafiles happend. This could be in character of a... (3 Replies)
Discussion started by: ruffi
3 Replies

8. Shell Programming and Scripting

Search and Replace using awk

Hi, I am really confused with this problem that I am facing . I have a file that contains entries in the form : option1 Value1 option2 Value2 option3 Value3 option4 Value4 . . . I want to search for the keyword "option4" and replace "value4" by another value, say "value5". My main... (2 Replies)
Discussion started by: harry0812
2 Replies

9. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

10. Shell Programming and Scripting

Search and replace using awk

Dear All, I want to search and replace the text in file using awk. but facing hard luck in that. Please help me out!!!! > grep Abc.De.ync.rate /tmp/sdosanjh.txt Abc.De.ync.rate 6 write Now, I want to replace the "6" with value say "2". I... (5 Replies)
Discussion started by: sdosanjh
5 Replies
Login or Register to Ask a Question