awk match to update contents of file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk match to update contents of file
# 1  
Old 09-06-2016
awk match to update contents of file

I am trying to match $1 in file1 with $2 in file2. If a match is found then $3 and $4 of file2 are copied to file1. Both files are tab-delimeted and I am getting a syntax error and would also like to update file1 in-place without creating a new file, but am not sure how. Thank you Smilie.

file1
Code:
123
456
789
111
222
333
444
555

file2
Code:
...    789    yyy    xxx    zzz    11111
...    111    xxx    yyy    aaa    22222
...    222    zzz    xxx    yyy    33333

awk
Code:
awk -F'\t' 'NR==FNR{A[$1]=$2; next} $2 in A{$3=A[$3] $3 in A{$4=A[$4]}}1' file1 file2 > output
awk: cmd. line:1: NR==FNR{A[$1]=$2; next} $2 in A{$3=A[$3] $3 in A{$4=A[$4]}}1
awk: cmd. line:1:

desired output (file1 updated to)
Code:
...    789    yyy    xxx
...    111    xxx    yyy
...    222    zzz    xxx


Last edited by cmccabe; 09-06-2016 at 06:34 PM.. Reason: added error and fixed format
# 2  
Old 09-06-2016
So, to be clear, your desired output seems to indicate that lines in file1 that do not have a match in file2 are to be deleted and lines that do have a match are to be replaced by the first four fields in file2??? I didn't see anything about deleting non-matching lines in your description and I didn't see anything about copying field 1 from file2 to file1 in your description.
This User Gave Thanks to Don Cragun For This Post:
# 3  
Old 09-06-2016
The lines in file1 that do not have a match in file2 just need to be copied over. They do not need to be deleted.

updated output
Code:
123
456
...    789    yyy    xxx
...    111    xxx    yyy
...    222    zzz    xxx
333
444
555

Thank you and I apologize for that over-sight Smilie.
# 4  
Old 09-06-2016
Try
Code:
awk 'NR == FNR {T[$2] = $1 FS $2 FS $3 FS $4; next} $1 in T {$1 = T[$1]} 1' FS="\t" file2 file1
123
456
...	789	yyy	xxx
...	111	xxx	yyy
...	222	zzz	xxx
333
444
555

awk can't modify in place, you need to go via a temp file.
This User Gave Thanks to RudiC For This Post:
# 5  
Old 09-06-2016
At the expense of a little bit of memory during runtime, we can avoid a temp file:
Code:
awk '
BEGIN {	FS = OFS = "\t"
}
NR == 1 {
	outfile = FILENAME
}
FNR == NR {
	o[i[++ic] = $1] = $1
}
{	if($2 in o)
		o[$2] = $1 OFS $2 OFS $3 OFS $4
}
END {	for(j = 1; j <= ic; j++)
		print o[i[j]] > outfile
}' file1 file2

As always, if you want to try this on a Solaris/SunOS system, change awk to /usr/xpg4/bin/awk or nawk.
This User Gave Thanks to Don Cragun For This Post:
# 6  
Old 09-07-2016
Thank you both very much Smilie.

---------- Post updated at 10:00 AM ---------- Previous update was at 09:08 AM ----------

Since it is possible for the value in $1 to be non-unique I added o[i[++ic] = $1,$2,$3] =$1 multiple fields to the array, but the file is empty. Thank you Smilie.

file1
Code:
123     1     2
456     a     b
456     x     y
789     x     y

file2
Code:
456     x     y     z     1
789     x     y     z     2

awk
Code:
awk '
BEGIN {FS = OFS = "\t"
}
NR == 1 {
outfile = FILENAME
}
FNR == NR {
o[i[++ic] = $1,$2,$3] =$1        
}
{if($2 in o)
o[$2] = $1 OFS $2 OFS $3 OFS $4
}
END {for(j = 1; j <= ic; j++)
print o[i[j]] > outfile
}' file1 file2

desired result
Code:
123     1     2
456     a     b
456     x     y     z
789     x     y     z

# 7  
Old 09-07-2016
Both the codes giving me output below
Code:
123
456
789
111
222
333
444
555

I have recently installed Ubuntu in my laptop , do i need to update anything? . Please help. And the last code by cmccabe is giving blank lines in file1.

Last edited by looney; 09-07-2016 at 01:07 PM.. Reason: mention code of cmccabe
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update file based on match in 3 fields

Trying to use awk to store the value of $5 in file1 in array x. That array x is then used to search $4 of file1 to find aa match (I use x to skip the header in file1). Since $4 can have multiple strings in it seperated by a , (comma), I split them and iterate througn each split looking for a match.... (2 Replies)
Discussion started by: cmccabe
2 Replies

2. Shell Programming and Scripting

awk to update value based on pattern match in another file

In the awk, thanks you @RavinderSingh13, for the help in below, hopefully it is close as I am trying to update the value in $12 of the tab-delimeted file2 with the matching value in $1 of the space delimeted file1. I have added comments for each line as well. Thank you :). awk awk '$12 ==... (10 Replies)
Discussion started by: cmccabe
10 Replies

3. Shell Programming and Scripting

awk to update value in field of out file using contents of another Ask

In the out.txt below I am trying to use awk to update the contents of $9.. If $9 contains a + or - then $8 of out.txt is used as a key to lookup in $2 of file. When a match ( there will always be one) is found the $3 value of that file is used to update $9 of out.txt separated by a :. So the... (6 Replies)
Discussion started by: cmccabe
6 Replies

4. Shell Programming and Scripting

awk to update file based on partial match in field1 and exact match in field2

I am trying to create a cronjob that will run on startup that will look at a list.txt file to see if there is a later version of a database using database.txt as the source. The matching lines are written to output. $1 in database.txt will be in list.txt as a partial match. $2 of database.txt... (2 Replies)
Discussion started by: cmccabe
2 Replies

5. Shell Programming and Scripting

awk to update specific value in file with match and add +1 to specific digit

I am trying to use awk to match the NM_ in file with $1 of id which is tab-delimited. The NM_ will always be in the line of file that starts with > and be after the second _. When there is a match between each NM_ and id, then the value of $2 in id is substituted or used to update the NM_. Each NM_... (3 Replies)
Discussion started by: cmccabe
3 Replies

6. Shell Programming and Scripting

awk to update field in file based of match in another

I am trying to use awk to match two files that are tab-delimited. When a match is found between file1 $1 and file2 $4, $4 in file2 is updated using the $2 value in file1. If no match is found then the next line is processed. Thank you :). file1 uc001bwr.3 ADC uc001bws.3 ADC... (4 Replies)
Discussion started by: cmccabe
4 Replies

7. Shell Programming and Scripting

awk to update field file based on match

If $1 in file1 matches $2 in file2. Then the value in $2 of file2 is updated to $1"."$2 of file2. The awk seems to only match the two files but not update. Thank you :). awk awk 'NR==FNR{A ; next} $1 in A { $2 = a }1' file1 file2 file1 name version NM_000593 5 NM_001257406... (3 Replies)
Discussion started by: cmccabe
3 Replies

8. Shell Programming and Scripting

[Solved] Lookup a file and match the contents

Hi, I appreciate all who have been very helpful to me in providing valuable suggestions and replies. I want to write a script to look up a file and match the contents. Let me go through the scenario. Lets say i have two files Content file: abc, bcd, adh|bcdf|adh|wed bcf, cdf,... (2 Replies)
Discussion started by: forums123456
2 Replies

9. Shell Programming and Scripting

update file contents using shell script

Hi, I am having a file which contains as below Names(aaaa ,bbbb ,cccc ,dddd) now i want the file to be updated with new value 'eeee' as below Names(aaaa ,bbbb ,cccc ,dddd ,eeee) Is there a way to script this ? Thanks, (5 Replies)
Discussion started by: drams
5 Replies

10. Shell Programming and Scripting

How to update the contents in a file conditionally?

Hi All, I have a data file which has two columns Location and the Count. The file looks like this India 1 US 0 UK 2 China 0 What I have to do is whenever I fails to login to Oracle then I have to add 1 to the count for that location. Whenever my script fails to login to Oracle for a... (5 Replies)
Discussion started by: rajus19
5 Replies
Login or Register to Ask a Question