.

Help with some bash script

<<

morpheus063

User avatar

Sr. Member
Sr. Member

Posts: 393

Joined: Sun Jun 25, 2006 10:08 am

Location: Cochin - India

Post Wed Jan 30, 2008 1:02 pm

Help with some bash script

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?
Manu Zacharia
MVP (Enterprise Security), ISLA-2010 (ISC)², C|EH, C|HFI, CCNA, MCP,
Certified ISO 27001:2005 Lead Auditor

[b]There are 3 roads to spoil; women, gambling & hacking. The most pleasant with women, the quickest with gambling, but the surest is hacking - c0c0n
<<

dean

Post Wed Jan 30, 2008 1:16 pm

Re: Help with some bash script

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
<<

morpheus063

User avatar

Sr. Member
Sr. Member

Posts: 393

Joined: Sun Jun 25, 2006 10:08 am

Location: Cochin - India

Post Wed Jan 30, 2008 1:24 pm

Re: Help with some bash script

Thank you so much Dean.
Manu Zacharia
MVP (Enterprise Security), ISLA-2010 (ISC)², C|EH, C|HFI, CCNA, MCP,
Certified ISO 27001:2005 Lead Auditor

[b]There are 3 roads to spoil; women, gambling & hacking. The most pleasant with women, the quickest with gambling, but the surest is hacking - c0c0n
<<

dean

Post Wed Jan 30, 2008 1:37 pm

Re: Help with some bash script

No problem,

Also look into using AWK for tasks such as these.

dean
<<

morpheus063

User avatar

Sr. Member
Sr. Member

Posts: 393

Joined: Sun Jun 25, 2006 10:08 am

Location: Cochin - India

Post Thu Jan 31, 2008 12:55 am

Re: Help with some bash script

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
0000
4444
5555
7777
8888

The output should be
  Code:
4444
5555

I found the following script after googling,

  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 ~
$ ./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 ~

Thanks in advance
Manu Zacharia
MVP (Enterprise Security), ISLA-2010 (ISC)², C|EH, C|HFI, CCNA, MCP,
Certified ISO 27001:2005 Lead Auditor

[b]There are 3 roads to spoil; women, gambling & hacking. The most pleasant with women, the quickest with gambling, but the surest is hacking - c0c0n
<<

zr0crsh

Newbie
Newbie

Posts: 14

Joined: Tue Apr 24, 2007 8:31 am

Post Thu Jan 31, 2008 1:09 pm

Re: Help with some bash script

Here is the quick and dirty that might work for you:

  Code:
cat file1 |while read line; do grep ${line} file2 ; done
Last edited by zr0crsh on Mon Feb 04, 2008 2:03 pm, edited 1 time in total.

Return to Programming

Who is online

Users browsing this forum: No registered users and 0 guests

cron
.
Powered by phpBB® Forum Software © phpBB Group.
Designed by ST Software