Operating System - HP-UX
1748066 Members
5358 Online
108758 Solutions
New Discussion юеВ

Re: grep to compare two files...

 
sekar sundaram
Honored Contributor

grep to compare two files...

com, comm give issues...so, i am thinking of comparing two files with grep itself...

I am having two list of nodes. i want to compare them and make the list of nodes present in fair and missing in app1
ie,
grep $NODE app1-node-list >>present
if $NODE not present in app1 node list, i want to write it in a new file...how to do that please....

root@fair> ll
total 224
-rw-r--r-- 1 root sys 85757 Nov 12 01:47 app1-node-list
-rw-r--r-- 1 root sys 19198 Nov 12 08:25 fair-node-list
root@fair>
root@fair> for NODE in `cat fair-node-list`
> do
> grep $NODE app1-node-list >>present
> done
root@fair> ll
total 256
-rw-r--r-- 1 root sys 85757 Nov 12 01:47 app1-node-list
-rw-r--r-- 1 root sys 19198 Nov 12 08:25 fair-node-list
-rw-r--r-- 1 root sys 14017 Nov 12 09:28 present
root@fair> wc *
7637 7660 85757 app1-node-list
1576 1570 19198 fair-node-list
1192 1192 14017 present
10405 10422 118972 total
root@fair>
6 REPLIES 6
Hein van den Heuvel
Honored Contributor

Re: grep to compare two files...

well, you could just use :

grep -f app1-node-list fair-node-list

Beware that the lines in the first file mentioned are treated as regular expression PATTERNs.
So depending on needs, feeds and the exact platform being used you may want to add any or all of:
-i, ignore case distinctions
-w, force PATTERN to match only whole words
-x, force PATTERN to match only whole lines

sekar sundaram
Honored Contributor

Re: grep to compare two files...

looks like something wrong...

grep -f app1-node-list fairlane-node-list |wc
1576 1570 19198
Hein van den Heuvel
Honored Contributor

Re: grep to compare two files...


I missed out the NOT, for grep that is: -v

It this what you desire:

# cat fair-node-list
aap
noot
kees
# cat app1-node-list
aap
noot
mies
vuur
# grep -v -f app1-node-list fair-node-list
kees
#
James R. Ferguson
Acclaimed Contributor

Re: grep to compare two files...

Hi:

Using 'comm' assumes sorted files. Using Hein's files and their contents:

# comm -13 app1-node-list fair-node-list
kees

Regards!

...JRF...
sekar sundaram
Honored Contributor

Re: grep to compare two files...

Hi, can you tell me this issue --

#if $NODE is present in node.list, update that to present.txt
grep $NODE node.list >>present.txt
#if $NODE is not present in this node.list, how to check that condition? will this below if loop will work?
if [ `grep $NODE node.list` ==0 ]
then
echo $NODE >>not.present.txt
fi
Dennis Handly
Acclaimed Contributor

Re: grep to compare two files...

>will this below "if" work?

You need to clean it up a little:
grep -q $NODE node.list
if [ $? -ne 0 ]; then
echo $NODE >> not.present.txt
fi