LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Networking
User Name
Password
Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game.

Notices

Reply
 
Search this Thread
Old 08-04-2008, 08:53 AM   #1
pooppp
Member
 
Registered: May 2008
Posts: 32

Rep: Reputation: 15
How to Compare Two files using shell script


How to compare two files called "A.sh" and "B.sh" in unix shell script and the third file "C.sh" should contains the contents of "B.sh" which r not in "A.sh".

Plz help me

Last edited by pooppp; 08-04-2008 at 09:03 AM.
 
Old 08-04-2008, 09:42 AM   #2
brianmcgee
Member
 
Registered: Jun 2007
Location: Munich, Germany
Distribution: RHEL, CentOS, Fedora, SLES (...)
Posts: 399

Rep: Reputation: 38
Code:
# comm -1 -3 A.sh B.sh > C.sh
 
Old 08-04-2008, 10:45 PM   #3
pooppp
Member
 
Registered: May 2008
Posts: 32

Original Poster
Rep: Reputation: 15
Hai,
Above not working..
I want the third file(c.sh) should the contents of second file(b.sh) which will not be in first file(a.sh)

for eg:

a.sh
Hello World

b.sh
Hai john, How r u?
World is full of enthu.

c.sh
Hai john, How r u?
is full of enthu.

Note :In c.sh, the word "World" is not there bcoz tht word is in a.sh..

Plz help...
 
Old 08-04-2008, 11:27 PM   #4
pooppp
Member
 
Registered: May 2008
Posts: 32

Original Poster
Rep: Reputation: 15
Sorry..Above one is wrong....

I need script like below,
Script to compare file a & file b, the ouput file c should contains the matched contents of file a & file b

file a

1
2
3
4
-----------

file b

a
b
1
2
3
g
--------------
result

1
2
3
-------------
Plz help me
 
Old 08-04-2008, 11:39 PM   #5
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Try comm -2 a b > c

See man comm
 
Old 08-05-2008, 12:38 AM   #6
pooppp
Member
 
Registered: May 2008
Posts: 32

Original Poster
Rep: Reputation: 15
Not working above...
I read man & info of comm..but i m not getting the required things
 
Old 08-05-2008, 12:56 AM   #7
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,627

Rep: Reputation: Disabled
comm -3 a.sh b.sh > c.sh
 
Old 08-05-2008, 01:11 AM   #8
pooppp
Member
 
Registered: May 2008
Posts: 32

Original Poster
Rep: Reputation: 15
Hai,
I tried with above..its showing wrong as below

a.sh
1
2
3
4

-------
b.sh
1
2
3
---------
o/p
4

I want 1 2 3 in o/p but it giving 4 as o/p
 
Old 08-05-2008, 01:24 AM   #9
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,627

Rep: Reputation: Disabled
I tried the same thing. I created a file named 'a' with contents 12345
and file 'b' with contents 123.
This command gave me the common elements of both.

comm -1 a b
 
Old 08-05-2008, 01:34 AM   #10
pooppp
Member
 
Registered: May 2008
Posts: 32

Original Poster
Rep: Reputation: 15
Hai above is working but i redirected to another file as

comm -1 a b >> c

while running the cmd every time result is keep on adding to c..i want the current result only...how to do?

is the below script right?
for i in `cat fileA`
do
grep $i fileB
if [ $? -eq 0 ]
then
{
echo $i >> filec
}
fi
done

Its working fine. but the result is keep on adding to filec(o/p file)..how to clear the o/p file
 
Old 08-05-2008, 02:05 AM   #11
billymayday
Guru
 
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678

Rep: Reputation: 122Reputation: 122
Don't us >> use >
 
Old 08-05-2008, 02:05 AM   #12
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,627

Rep: Reputation: Disabled
comm -1 ab b > c
Use single redirector operator. This will not append the new result and rather over write the file.
 
Old 08-05-2008, 02:07 AM   #13
linuxlover.chaitanya
Senior Member
 
Registered: Apr 2008
Location: Nagpur, India
Distribution: Cent OS 5/6, Ubuntu Server 10.04
Posts: 4,627

Rep: Reputation: Disabled
OOps!!! seems billymayday and me wrote the post at the same time. same answer....same time.
 
Old 08-05-2008, 02:58 AM   #14
jiml8
Senior Member
 
Registered: Sep 2003
Posts: 3,171

Rep: Reputation: 114Reputation: 114
I think

comm -1 -2 a.sh b.sh >c.sh

is what you want.
 
Old 08-05-2008, 03:35 AM   #15
pooppp
Member
 
Registered: May 2008
Posts: 32

Original Poster
Rep: Reputation: 15
all ur answer r working fine..thnx
but for my script below,

for i in `cat fileA`
do
grep $i fileB
if [ $? -eq 0 ]
then
{
echo $i >> filec
}
fi
done

I don't want to print in screen i.e., instead of echo cmd above, i want to redirect to filec..
The abve script is working fine while using echo..but in filec its showing only last ele..
Plz edit the above script & send me as i need.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
shell script: compare 2 files anhtt Programming 6 08-29-2007 02:39 AM
shell script to recursively "compare" all files in a directory... silex_88 Programming 3 05-12-2007 04:24 AM
shell script to compare filese b/w local and remote and delete files from local serve dsids Linux - Networking 9 08-23-2006 07:20 AM
compare files in C shell shashwat.gupta Programming 8 05-24-2006 02:13 AM
Shell script to compare blocks of strings? bruno buys Programming 10 04-15-2006 02:16 PM


All times are GMT -5. The time now is 05:33 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration