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

Search 2 Lists 2

Status
Not open for further replies.

Mag0007

MIS
Feb 15, 2005
829
US
I have a 2 files.

File A has this:
1234
4321
3245

File B has this:
4321
3245
1233
1234

I want to see if File A has the same data (not order) as File B
 
cmp

$ cmp filea fileb
filea fileb differ: char 4, line 1
$ echo $?
1
$ cp filea fileb
$ cmp filea fileb
$ echo $?
0

 
The command [tt]comm[/tt] can be used to compare the files and show you the differences between them. The files need to be sorted first, but [tt]comm[/tt] will give three columns of output, 1) lines only in filea, 2) lines only in fileb, and 3) lines in both files. You can supply a command line option to suppress any of these columns to make a file with what you want.

Assuming they're sorted...
Code:
$ comm fileA fileB
        1233
                1234
                3245
                4321
$# Lines common to both
$ comm -12 fileA fileB
1234
3245
4321
$# Lines only in fileB
$ comm -13 fileA fileB
1233
$# Lines only in fileA
$ comm -23 fileA fileB
$
Hope this helps.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top