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!

Comparing items in 2 files

Status
Not open for further replies.

omoo

Programmer
May 30, 2005
87
0
0
US
Hi,

I have 2 files with contents in them and I need to compare each item in each file.

File1:
item4
item5

File2:
item2
item3
item5
item6

The items names can be of different lengths.
If the items in the File1 are not in File2,
delete the missing item in File1.

The resulting File1 in the above example should be:
File1:
item5

How can I do this using shell script? Thanks in advance!
 
Without actually writing the script for you, here are the steps I would use to do this:
Read an item from File1 (line by line)
Check to see if it is in File2
If it is, then write the item to File3 (if not then don't)
Repeat to the end of File1
Overwrite File1 with File3 (if you want to)

Check out the man pages for your 'shell' and commands like while and grep.

I hope that helps.

Mike
 
The [tt]comm[/tt] command does that for you. The files have to be sorted though.
Code:
$ cat file1
item4
item5
$ cat file2
item2
item3
item5
item6
$ comm -12 file1 file2
item5
See the [tt]man[/tt] page for more info.

Hope this helps.
 
I need the original data in file1 to remain if it is not in file2 but comm command will delete them. For example,

file1:
item5
item9
item10
item2

file2:
item1
item2
item3
item5
item6
item7

correct new file1:
item5
item2

using comm, new file1:
item5
 
To use the comm command 'properly' the files have to be sorted. Note that in your example file1 is not. My 'pseudo-script' above will maintain File3 in the order of File1, and then you can choose whether to overwrite File1 at the end.

I hope that helps.

Mike
 
The command [tt]comm[/tt] doesn't delete anything. And Mike is right, the files must be sorted first.

With them sorted...
Code:
$ cat file1
item10
item2
item5
item9
$ cat file2
item1
item2
item3
item5
item6
item7
$ comm -12 file1 file2 > file3
$ cat file3
item2
item5
This is what you want I believe.

Hope this helps.
 
Files don't need to sorted if you use grep -f
Code:
$ [b]grep -f file1 file2[/b]
item2
item5
 
That is only if you are using the xpg4 version.
Code:
/usr/xpg4/bin/grep -f file1 file2
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top