Image
 
linkedin_logo.png rss_logo.jpg
twitter_logo.png youtube_logo.jpg
Latest Additions
 
EH-Net Login
Welcome Guest.






Lost Password?
No account yet? Register
Who's Online
We have 35 guests and 1 member online
 
Free Business and Tech Magazines and eBooks

You are here: Home arrow Ethical Hacking Discussions and Related Certificationsarrow Programmingarrow Help with some bash script
EH-Net
May 21, 2013, 02:38:09 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Go back to The Ethical Hacker Network Online Magazine Home Page
 
   Home   Help Calendar Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Help with some bash script  (Read 6347 times)
0 Members and 1 Guest are viewing this topic.
Manu Zacharia (-M-)
Sr. Member
****
Offline Offline

Posts: 393


c0c0n Hacking Conference - where hackers unite


View Profile WWW
« 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?
Logged

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

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
Guest
« Reply #1 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
Logged
Manu Zacharia (-M-)
Sr. Member
****
Offline Offline

Posts: 393


c0c0n Hacking Conference - where hackers unite


View Profile WWW
« Reply #2 on: January 30, 2008, 12:24:27 PM »

Thank you so much Dean.
Logged

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

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
Guest
« Reply #3 on: January 30, 2008, 12:37:02 PM »

No problem,

Also look into using AWK for tasks such as these.

dean
Logged
Manu Zacharia (-M-)
Sr. Member
****
Offline Offline

Posts: 393


c0c0n Hacking Conference - where hackers unite


View Profile WWW
« Reply #4 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
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
Logged

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

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

Posts: 14


View Profile
« Reply #5 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
« Last Edit: February 04, 2008, 01:03:07 PM by zr0crsh » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines
Joomla Bridge by JoomlaHacks.com
Valid XHTML 1.0! Valid CSS!
Page created in 0.05 seconds with 23 queries.
 
Exclusive Deal

sansfire13_245x90_cw90.jpg
SANSFIRE 2013
June 15 - 22

5% Off w/ Code: EHN_5

SANS Deals 4 EH-Netters
5% OFF Any SANS Course in Any Format!
Coupon Code: EHN_5 Including SANS Rocky Mountain 2013 & SANS Boston 2013
Polls
Compared to this year, 2013 will be:
 
Recent Forum Topics
EH-Net News Feeds
Latest Additions
 
         
Free Business and Tech Magazines and eBooks

© 2013 The Ethical Hacker Network
Joomla! is Free Software released under the GNU/GPL License.