Thursday, February 14, 2008

Finding common numbers between two files - BASH


Simple, but very^2 important.

$ cat file1
123
123
321
342
546
523
652

$ cat file2
143
767
984
123
342

Sort the files in ascending order:

$ sort -o file1.srt file1
$ sort -o file2.srt file2

----------------------------------
$ comm --help
Usage: comm [OPTION]... LEFT_FILE RIGHT_FILE
Compare sorted files LEFT_FILE and RIGHT_FILE line by line.

-1 suppress lines unique to left file
-2 suppress lines unique to right file
-3 suppress lines unique to both files
--help display this help and exit
--version output version information and exit

Report bugs to .
----------------------------------

$ comm -12 file1.srt file2.srt
123
342

or

Without sorting, we have an easy option with "grep"

$ grep -f file1 file2
123
342

No comments:

© Jadu Saikia www.UNIXCL.com