Linux - Networking This forum is for any issue related to networks or networking.
Routing, network cards, OSI, etc. Anything is fair game. |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
|
08-04-2008, 09:53 AM
|
#1
|
Member
Registered: May 2008
Posts: 32
Rep:
|
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 10:03 AM.
|
|
|
08-04-2008, 10:42 AM
|
#2
|
Member
Registered: Jun 2007
Location: Munich, Germany
Distribution: RHEL, CentOS, Fedora, SLES (...)
Posts: 399
Rep:
|
Code:
# comm -1 -3 A.sh B.sh > C.sh
|
|
|
08-04-2008, 11:45 PM
|
#3
|
Member
Registered: May 2008
Posts: 32
Original Poster
Rep:
|
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...
|
|
|
08-05-2008, 12:27 AM
|
#4
|
Member
Registered: May 2008
Posts: 32
Original Poster
Rep:
|
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
|
|
|
08-05-2008, 12:39 AM
|
#5
|
LQ Guru
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678
Rep:
|
Try comm -2 a b > c
See man comm
|
|
|
08-05-2008, 01:38 AM
|
#6
|
Member
Registered: May 2008
Posts: 32
Original Poster
Rep:
|
Not working above...
I read man & info of comm..but i m not getting the required things
|
|
|
08-05-2008, 01:56 AM
|
#7
|
Senior Member
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,638
Rep:
|
comm -3 a.sh b.sh > c.sh
|
|
|
08-05-2008, 02:11 AM
|
#8
|
Member
Registered: May 2008
Posts: 32
Original Poster
Rep:
|
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
|
|
|
08-05-2008, 02:24 AM
|
#9
|
Senior Member
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,638
Rep:
|
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
|
|
|
08-05-2008, 02:34 AM
|
#10
|
Member
Registered: May 2008
Posts: 32
Original Poster
Rep:
|
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
|
|
|
08-05-2008, 03:05 AM
|
#11
|
LQ Guru
Registered: Mar 2006
Location: Sydney, Australia
Distribution: Fedora, CentOS, OpenSuse, Slack, Gentoo, Debian, Arch, PCBSD
Posts: 6,678
Rep:
|
Don't us >> use >
|
|
|
08-05-2008, 03:05 AM
|
#12
|
Senior Member
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,638
Rep:
|
comm -1 ab b > c
Use single redirector operator. This will not append the new result and rather over write the file.
|
|
|
08-05-2008, 03:07 AM
|
#13
|
Senior Member
Registered: Apr 2008
Location: Gurgaon, India
Distribution: Cent OS 6/7
Posts: 4,638
Rep:
|
OOps!!! seems billymayday and me wrote the post at the same time. same answer....same time.
|
|
|
08-05-2008, 03:58 AM
|
#14
|
Senior Member
Registered: Sep 2003
Posts: 3,171
Rep:
|
I think
comm -1 -2 a.sh b.sh >c.sh
is what you want.
|
|
|
08-05-2008, 04:35 AM
|
#15
|
Member
Registered: May 2008
Posts: 32
Original Poster
Rep:
|
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.
|
|
|
All times are GMT -5. The time now is 07:14 PM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|