Awk Compare File1 File2 on f2


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk Compare File1 File2 on f2
# 1  
Old 10-26-2008
Awk Compare File1 File2 on f2

I'm trying to compare two files using AWK, where if field2 of both files match, replace field1 of file1 with field1 of file2 and if there is no match just print the line of file1.

file1.txt (has empty first field)
Code:
:ABBATOM:B:H:1992
:ABBA TROJAN:B:H:1993
:ABBES FIRST HOPE:B:M:1997
:ABBEYS LENDING:BR:M:1996
:ABBEYS LEGACY:BR:M:1997
:ABBIE VELMA:B:M:1992
:ABBIESRUN:B:M:1995

file2.txt
Code:
31:ABBATOM:B:H:1992
32:ABBA TROJAN:B:H:1993
35:ABBIE VELMA:B:M:1992
36:ABBIESRUN:B:M:1995

DESIRED OUTPUT
Code:
31:ABBATOM:B:H:1992
32:ABBA TROJAN:B:H:1993
:ABBES FIRST HOPE:B:M:97
:ABBEYS LENDING:BR:M:1996
:ABBEYS LEGACY:BR:M:1997
35:ABBIE VELMA:B:M:1992
36:ABBIESRUN:B:M:1995

Any help would be appreciated.

Last edited by RacerX; 10-26-2008 at 09:36 PM.. Reason: made a boo boo
# 2  
Old 10-26-2008
You should try to solve your problem before asking for help:
Code:
awk -F':' 'NR==FNR{_[$2]=$1;next}$2 in _{$1=_[$2]}1' OFS=':' f2 f1

# 3  
Old 10-27-2008
Thanks, i did try to solve it and did many searches here using keywords awk and compare. However, i kept screwing it up, am very confused at this point, and got nowhere and i really don't want to mess-up my 743,000 record database file, with something like:

Code:
FNR==NR { arr[$2]=$1; next}
$2 in arr { print arr[$1], $0 }

if (field1 of file2) == (field2 of file1) then
awk -F: 'FNR == NR {
 { f1[$2]=$1 $2
  next;
  }
if ($2 in f1) print  f1[$1]$0; else print $0;}' ~/Desktop/file2.txt ~/Desktop/file1.txt > ~/Desktop/outputfile.txt

# 4  
Old 10-27-2008
Quote:
Originally Posted by RacerX
.. i really don't want to mess-up my 743,000 record database file, with something like:
Always backup your working file.

Quote:
Originally Posted by RacerX
Code:
FNR==NR { arr[$2]=$1; next}
$2 in arr { print arr[$1], $0 }

This will print only if $2 is in arr.


Quote:
Originally Posted by RacerX
Code:
if (field1 of file2) == (field2 of file1) then   #why you need this test?
awk -F: 'FNR == NR {
 { f1[$2]=$1 $2                                  # why you need the value of $2
  next;
  }
if ($2 in f1) print  f1[$1]$0; else print $0
}
' ~/Desktop/file2.txt ~/Desktop/file1.txt > ~/Desktop/outputfile.txt

# 5  
Old 10-27-2008
Quote:
Always backup your working file.
Yes, words to live by, and backups have saved my rear-end many a time.

Quote:
if (field1 of file2) == (field2 of file1) then #why you need this test?
Sorry was thinking out loud and that line was actually commented out in program.

Quote:
{ f1[$2]=$1 $2 # why you need the value of $2
Told you i was totally confused. Smilie

Your solution worked and is much more elegant than anything i was working on:
Code:
awk -F':' 'NR==FNR{_[$2]=$1;next}$2 in _{$1=_[$2]}1' OFS=':' f2 f1

Thanks.
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 field in file2 if not the same as file1

Trying to use awk to: update $2 in file2 with the $2 value in file1, if $1 in file1 matches $13 in file2, which is tab-delimeted. The $2values may already be the same so in that case nothing happens and the next line is processed. There are exactly 4,605 unique $13 values. Thank you :). ... (4 Replies)
Discussion started by: cmccabe
4 Replies

2. Shell Programming and Scripting

awk to search field2 in file2 using range of fields file1 and using match to another field in file1

I am trying to use awk to find all the $2 values in file2 which is ~30MB and tab-delimited, that are between $2 and $3 in file1 which is ~2GB and tab-delimited. I have just found out that I need to use $1 and $2 and $3 from file1 and $1 and $2of file2 must match $1 of file1 and be in the range... (6 Replies)
Discussion started by: cmccabe
6 Replies

3. UNIX for Dummies Questions & Answers

Compare file1 and file2, print matching lines in same order as file1

I want to print only the lines in file2 that match file1, in the same order as they appear in file 1 file1 file2 desired output: I'm getting the lines to match awk 'FNR==NR {a++}; FNR!=NR && a' file1 file2 but they are in sorted order, which is not what I want: Can anyone... (4 Replies)
Discussion started by: pathunkathunk
4 Replies

4. Shell Programming and Scripting

Compare and find records of file1 not in file2

hi.. i am using solaris system and ksh and using nawk to get records of file1 not in file2(not line by line comparison). code i am using is nawk 'NR==FNR{a++} !a {print"line:" FNR"->" $0} ' file2 file1 same command with awk runs perfectly on darwin kernel(mac) but in solaris it does line by... (2 Replies)
Discussion started by: Abhiraj Singh
2 Replies

5. Shell Programming and Scripting

Looking for lines, which is present in file1 but not in file2 using UNIX and awk

I have 2 files with 7 fields and i want to print the lines which is present in file1 but not in file2 based on field1 and field2. Logic: I want to print all the lines, where there is a particular column1 and column2. And we do not find the set of column1 and column2 in file2. Example: "sc2/10... (3 Replies)
Discussion started by: NamS
3 Replies

6. Shell Programming and Scripting

[awk] split file1 and save it as var from file2

I have 2 files: file_1: file_2: expected result: name file: "artV1" "artV2" etc. I have: but why don;t work save to file 'out'?? (3 Replies)
Discussion started by: ffresz
3 Replies

7. UNIX for Dummies Questions & Answers

if matching strings in file1 and file2, add column from file1 to file2

I have very limited coding skills but I'm wondering if someone could help me with this. There are many threads about matching strings in two files, but I have no idea how to add a column from one file to another based on a matching string. I'm looking to match column1 in file1 to the number... (3 Replies)
Discussion started by: pathunkathunk
3 Replies

8. Shell Programming and Scripting

AWK: Pattern match between 2 files, then compare a field in file1 as > or < field in file2

First, thanks for the help in previous posts... couldn't have gotten where I am now without it! So here is what I have, I use AWK to match $1 and $2 as 1 string in file1 to $1 and $2 as 1 string in file2. Now I'm wondering if I can extend this AWK command to incorporate the following: If $1... (4 Replies)
Discussion started by: right_coaster
4 Replies

9. Shell Programming and Scripting

awk - replacing stings in file1 with column1 in file2

Hello, I've never used awk before, but from what I've read, it will best suit what I'm trying to do. I have 2 files. I need to replace strings in file1 with the first column of a matching string in file2. Below are examples: File1: random-string1 1112 1232 3213 2131 random-string2... (7 Replies)
Discussion started by: upstate_boy
7 Replies

10. Shell Programming and Scripting

Awk Compare f1,f2,f3 of File1 with f1 of File2

I have an Awk string-compare problem and have searched the internet and forums for a solution i could use but cannot find a solution i understand to make work with my particular problem: I need to compare (field1 field2 field3 of File1) against (field1 of File2) and if they match print out... (6 Replies)
Discussion started by: RacerX
6 Replies
Login or Register to Ask a Question