Sunday, May 25, 2008

Finding Common Occurence in Files and Writing to a New file !

I wanted to compare lines in 2 files and write things which
are common in a different file.

Eg,
File1 (has line like below)
===========================
host1
host2
host3
etc.....


File2 (has line like below)
===========================
host2
host3
host4
host5
etc ...

Which ever is common among file1 and file 2 should be written to file3

Grep does it :

grep -f file1 file2 > file3

file3 will have host2, host3 from above example shown under it.