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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compare contents of 2 files and output only strings common to both

Status
Not open for further replies.

brk6971

Technical User
Aug 10, 2007
9
0
0
US
I have 2 files I need to compare the contents of and create a third file that contains only what is common to both files. Below is an example, the actual files contain thousands of entries.

file 1
G00127
G00653
G00737
G00744

File 2
G00127
G00252
G01328

File 3 (desired output)
G00127





 
Sorry - I'm about to leave, but have you read up on comm

Some days are diamonds, some days are rocks - make sure most are the former.
 
Hi

If your [tt]grep[/tt] supports -f or --file= option ( -F and -x can be ignored in certain circumstances, for example your sample data ) :
Code:
grep -F -x -f file1 file2 > file3

[gray]# or[/gray]

diff --old-line-format= --new-line-format= --unchanged-line-format='%L' file1 file2 > file3
Tested with GNU [tt]grep[/tt] and GNU [tt]diff[/tt].


Feherke.
 
That did the trick....Thanks for responding so quickly
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top