|
Title: Help with some bash script Post by: Manu Zacharia (-M-) on January 30, 2008, 12:02:22 PM Hi,
I need some help with bash scripting. I have two files and the contents are as follows: File1 Code: 1111 2222 4444 5555 4444 6666 File 2 Code: 9999 0000 4444 5555 7777 8888 I am looking for a script that will compare the two files or two list and output the common line that is available on both the files. In the above example, the output should be Code: 4444 5555 Can anybody help me with this? Title: Re: Help with some bash script Post by: dean on January 30, 2008, 12:16:21 PM why use a script? The commands for this exist already.
shell> comm -1 -2 file1.txt file2.txt This will display lines that are the same in both. dean Title: Re: Help with some bash script Post by: Manu Zacharia (-M-) on January 30, 2008, 12:24:27 PM Thank you so much Dean.
Title: Re: Help with some bash script Post by: dean on January 30, 2008, 12:37:02 PM No problem,
Also look into using AWK for tasks such as these. dean Title: Re: Help with some bash script Post by: Manu Zacharia (-M-) on January 30, 2008, 11:55:25 PM Hi
The comm does a line by line compare. I am looking for a compare where each line (entry) from file 1 is compared with all the lines on the second file and whatever value is common on both files is displayed. For example, file1 contains Code: 3322 1212 1111 2222 4444 5555 4545 6666 and file 2 contains Code: 9999 The output should be 0000 4444 5555 7777 8888 Code: 4444 I found the following script after googling, 5555 Code: comm file1.txt file2.txt | awk -F'\t' ' {a[NF,++i[NF]]=$NF} END{ print "Common\tFile1\tFile2" for(j=1;j<=i[1]||j<=i[2]||j<=i[3];++j) print a[3,j]"\t"a[1,j]"\t"a[2,j] print "===\t===\t===" print i[3]"\t"i[1]"\t"i[2] }' but the above script is giving the wrong output, not working for me. The output is as follows: Code: user@sysadmn ~ $ ./compare.sh Common File1 File2 3322 9999 1212 0000 1111 4444 2222 5555 4444 7777 5555 8888 4545 6666 === ==== ==== 8 6 user@sysadmn ~ The desired output is: Code: user@sysadmn ~ Thanks in advance$ ./compare.sh Common File1 File2 4444 3322 9999 5555 1212 0000 1111 4444 2222 5555 4444 7777 5555 8888 4545 6666 === === === 8 6 user@sysadmn ~ Title: Re: Help with some bash script Post by: zr0crsh on January 31, 2008, 12:09:15 PM Here is the quick and dirty that might work for you:
Code: cat file1 |while read line; do grep ${line} file2 ; done
Powered by SMF 1.1.18 |
SMF © 2013, Simple Machines
Joomla Bridge by JoomlaHacks.com |