LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 04-24-2009, 08:57 AM   #1
learnerlearner
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Rep: Reputation: 0
Read line by line and compare


Hi everyone...
I have a script for comparing RPMs installed as below:

#!/bin/bash
##VARIABLES used below are defined in my profile and am importing them before i use them
rm -f list.log
#Extract the RPMs from RPM Database on each of the server (there are around 20 servers)
for i in $SERVER; do
echo -n "$i" >> list.log
echo "" >> list.log
ssh $i 'rpm -qa | grep '$CURRENT_VER' ' >> list.log
done

#Extract Info from the Delivery Server (this is the master server from where i take the RPMs for installing on other servers)
rm -f $DELIVERY_LOG
for j in $WEBDAV_SERVER; do
echo $DELIVER_DIR
ssh <user_name>@$j 'find '$DELIVER_DIR' -type f -name "vst3*.rpm" -printf %f\\n ' >> $DELIVERY_LOG
done
sort delivery.log | sort -ud >> $FINAL_LOG

I am successful in comparing the files $FINAL_LOG and list.log by using a simple for loop like:
for line in `cat list.log`
do
grep -i $line final.log >/dev/null
exitcode=$?
if [ $exitcode = 0 ]; then
echo "$line: RPM exists in both files" >> mylog
else
echo "$line: RPM does not exist" >>mismatch
fi
done

Now, i need to know is the correct way of doing it? and also i need to know how i should compare list.log with another property file which has what RPM needs to go to which server

The property files contains something like this:

*Server1
abc
xyz
...
...
*Server2
abc
xyz
def
uif
...
...
*Server3
abc
xyz
fgh

and it goes on for each of the 20 servers

Even the list.log is sorted as u can see in the script above.
Can anyone please help me in doing this?

Thanks in advance
 
Old 04-24-2009, 11:54 AM   #2
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
I would use 'comm' to do this. If you want an example of how it can be used I used it rather innovatively in a script I made recently:
http://www.linuxquestions.org/questi...script-656705/
http://draconishinobi.50webs.com/slackware/usu.tgz

Either way look at 'man comm'. The lists need to be sorted for this to work properly. Another way is using paste and awk, as demonstrated in the script I wrote. I studied this problem for a while and I think these solutions are the fastest and best.
 
Old 05-06-2009, 07:05 AM   #3
learnerlearner
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Compare two files

Quote:
Originally Posted by H_TeXMeX_H View Post
I would use 'comm' to do this. If you want an example of how it can be used I used it rather innovatively in a script I made recently:
http://www.linuxquestions.org/questi...script-656705/
http://draconishinobi.50webs.com/slackware/usu.tgz

Either way look at 'man comm'. The lists need to be sorted for this to work properly. Another way is using paste and awk, as demonstrated in the script I wrote. I studied this problem for a while and I think these solutions are the fastest and best.
Hi,
Thanks for the reply...

But the comm command gives a weird output for me. It is having man blank spaces and the output file has entries of both the file :-(.
Is there any alternative you can suggest me in doing this?

Thanks,

Last edited by learnerlearner; 05-06-2009 at 07:07 AM.
 
Old 05-06-2009, 07:46 AM   #4
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
So, basically if you wanted to output all the entries present in both lists you would do:

Code:
comm -12 list1 list2 > list3
list3 would contain entries common to list1 and list2. There should be no spaces when used like this.

It would be useful if you post a more concrete example of what you want to do. So far, I'm guessing that you have 2 lists of the names of RPM packages and you want to list packages present on both lists ... well the above example should work.

The more tedious way would be to iterate over one file and see if there is a match in the other using grep like you did above except I would use the 'grep -x' option for better results.

Last edited by H_TeXMeX_H; 05-06-2009 at 07:48 AM.
 
Old 05-07-2009, 03:30 AM   #5
learnerlearner
LQ Newbie
 
Registered: Apr 2009
Posts: 6

Original Poster
Rep: Reputation: 0
Read line by line and compare

Quote:
Originally Posted by H_TeXMeX_H View Post
So, basically if you wanted to output all the entries present in both lists you would do:

Code:
comm -12 list1 list2 > list3
list3 would contain entries common to list1 and list2. There should be no spaces when used like this.

It would be useful if you post a more concrete example of what you want to do. So far, I'm guessing that you have 2 lists of the names of RPM packages and you want to list packages present on both lists ... well the above example should work.

The more tedious way would be to iterate over one file and see if there is a match in the other using grep like you did above except I would use the 'grep -x' option for better results.

Thanks for the reply..
So, the concrete example would be like this!!

I have the list.log created from the script which has the RPM packages on each server in the format as shown below
Server1
abc
xyz
...
...
Server2
abc
xyz
def
uif
...
...
Server3
abc
xyz
fgh

Now, i have created a property file which becomes a manual input to the script and which acts as a master file. This property file contains all the RPMs
that is currently used and it looks as below

abc:Server1,Server2
def:Server2,Server3,Server3,Server4
efg:Server2,Server3,Server3,Server4,Server6,Server7

What i need now is how can i read each of the server name from the property file and look into the list.log which is sorted as shown above
and compare under each of the server name entry of list.log. And the output should be something like
RPM abc is missing in <Server_name>

can you please help me out in this?

Thanks in Advance
 
Old 05-07-2009, 10:49 AM   #6
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
I see now what you want to do, and it's going to be more complicated.

You can do things to make it more plausible, unless someone here can whip out a massively complicated and convoluted one-liner for this:

1) Instead of generating list.log put each server list in a separate file, that way you can iterate through the property file and grep for the package in the corresponding log file.

2) Rearrange or generate the property file differently so that it is in a similar format as the list.log, most likely each server in a different file. This would be the optimal way, because you could use comm and it would be way faster that a loop.
 
  


Reply

Tags
compare, line


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
help with c program to read each line from text file, split line , process and output gkoumantaris Programming 12 07-01-2008 12:38 PM
php - Read file line by line and change a specific line. anrea Programming 2 01-28-2007 01:43 PM
How do I compare files line-by-line? spiffytech Linux - Software 2 01-05-2006 02:28 PM
File reading line by line and compare Goni Linux - Software 14 09-21-2005 12:24 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 10:24 PM


All times are GMT -5. The time now is 08: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