Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

compare 2 files and print the common content of file1 1

Status
Not open for further replies.

jdhahbi

Technical User
Oct 7, 2009
24
0
0
US
Hi
I would like lines from file1 that are common to file2 as in the following example:
Code:
cat file1
22	A	alp	19	gtein	
255	B	macr	12	pring	
3	C	lin	12	pseue	
90	D	N-ace	8	1
Code:
cat file2
B
D
Code:
cat outfile
255	B	macr	12	pring	
90	D	N-ace	8	1

thank you for your help
 
With awk, you'll need to put file2 in an array and then print records from file1 if field 2 is on that array.

That's easier if you process file2 first, then file1. Google around for "awk array". Chances are you'll get yourself right back in this forum ;-).

But there's a neat solution with the UNIX join command:

Code:
join -1 2 -2 1 file1 file2

Only glitch is that the output field separator is (by default) one space character. But if you can live with that...

HTH,

p5wizard
 
I'm trying to learn awk, so I done it as exercise
join.awk
Code:
[COLOR=#0000ff]# Run:[/color]
[COLOR=#0000ff]# awk -f join.awk file1.txt file2.txt[/color]
[COLOR=#6a5acd]BEGIN[/color] {
  [COLOR=#6a5acd]FS[/color] = [COLOR=#ff00ff]" "[/color]
  lines_found = [COLOR=#ff00ff]0[/color]
}
{ 
  [COLOR=#804040][b]if[/b][/color] ([COLOR=#6a5acd]FILENAME[/color] == [COLOR=#6a5acd]ARGV[/color][[COLOR=#6a5acd]1[/color]]) {
    [COLOR=#0000ff]# mark line from 1.file in array[/color]
    line_array[[COLOR=#6a5acd]$1[/color]] = [COLOR=#ff00ff]1[/color]
  }
  [COLOR=#804040][b]if[/b][/color] ([COLOR=#6a5acd]FILENAME[/color] == [COLOR=#6a5acd]ARGV[/color][[COLOR=#6a5acd]2[/color]]) {
    [COLOR=#0000ff]# print adequate lines from 2.file[/color]
    [COLOR=#804040][b]if[/b][/color] (line_array[[COLOR=#6a5acd]$2[/color]]) {
      [COLOR=#804040][b]print[/b][/color] [COLOR=#6a5acd]$0[/color]
      lines_found ++
    }
  }
}
[COLOR=#6a5acd]END[/color] {
  [COLOR=#804040][b]printf[/b][/color] [COLOR=#ff00ff]"There were [/color][COLOR=#6a5acd]%d[/color][COLOR=#ff00ff] lines found and printed.[/color][COLOR=#6a5acd]\n[/color][COLOR=#ff00ff]"[/color][COLOR=#6a5acd],[/color] lines_found
}
Output:
Code:
$ awk -f join.awk file1.txt file2.txt
255    B    macr    12    pring    
90    D    N-ace    8    1
There were 2 lines found and printed.
I changed the file order i.e.
file1.txt
Code:
B
D
file2.txt
Code:
22    A    alp    19    gtein    
255    B    macr    12    pring    
3    C    lin    12    pseue    
90    D    N-ace    8    1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top