User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Shell Scripting section within the Software Development category of DaniWeb, a massive community of 456,446 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,659 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Shell Scripting advertiser: Programming Forums
Views: 1495 | Replies: 10
Reply
Join Date: Oct 2007
Posts: 22
Reputation: dave_nithis is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Solution Challenging Script

  #1  
Nov 16th, 2007
Hi all,

I have two files namely
a.txt
b.txt
a.txt contains 100 records and b.txt contains 9 records.These 9 records in b.txt is also present in a.txt.So,I need a script that compares these two files and outputs the unique 91 records alone in another file.

Thanks to all in advance!!!!

Regards
dave.
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Feb 2006
Posts: 1,509
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: Challenging Script

  #2  
Nov 16th, 2007
Cool. What do you have so far, and what problem are you having with it.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Oct 2007
Posts: 22
Reputation: dave_nithis is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Re: Challenging Script

  #3  
Nov 16th, 2007
I am trying to do this.But i dont know how to do the comparison part.
Reply With Quote  
Join Date: Oct 2007
Posts: 22
Reputation: dave_nithis is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Re: Challenging Script

  #4  
Nov 16th, 2007
Also am trying this in microsoft Access.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,509
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: Challenging Script

  #5  
Nov 16th, 2007
Originally Posted by dave_nithis View Post
Also am trying this in microsoft Access.


What do you mean by this? This forum is for Linux/Unix Shell Scripting, not Windows Batching (unless things have changed).

As far as doing the comparisons, an easy, but not necessarily performant way is a simple grep command.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Oct 2007
Posts: 306
Reputation: eggi is on a distinguished road 
Rep Power: 2
Solved Threads: 29
eggi eggi is offline Offline
Posting Whiz

Re: Challenging Script

  #6  
Nov 17th, 2007
a's got 100
b's got 9
you need the 91 uniques - I'll put 'em in c.txt

while read line
do
   grep $line b.txt >/dev/null 2>&1               
   if [ $? -ne 0 ]
   then
       echo $line >>c.txt
   fi
done <a.txt

Sorry - just needed to drop a post somewhere - it's been a while

, Mike
Linux and Unix Tips, Tricks and Individual Advice - The Linux and Unix Menagerie!
------------------------------------------------------------------------
Having trouble passing cert exams? Check out How To Pass Any Computer Certification Test!
Reply With Quote  
Join Date: Feb 2006
Posts: 1,509
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: Challenging Script

  #7  
Nov 17th, 2007
Originally Posted by eggi View Post
a's got 100
b's got 9
you need the 91 uniques - I'll put 'em in c.txt

while read line
do
   grep $line b.txt >/dev/null 2>&1               
   if [ $? -ne 0 ]
   then
       echo $line >>c.txt
   fi
done <a.txt

Sorry - just needed to drop a post somewhere - it's been a while

, Mike


Its actually easier, if with a few more file operations.

cp b.txt /tmp/b1.txt
while read line
do
   grep -v $line /tmp/b1.txt >/tmp/b2.txt 2>/dev/null
   mv -f b2.txt b1.txt
done <a.txt
mv b1.txt c.txt
Last edited by masijade : Nov 17th, 2007 at 5:58 am.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Join Date: Oct 2007
Posts: 22
Reputation: dave_nithis is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Re: Challenging Script

  #8  
Nov 19th, 2007
Masijade,

I finished the file comparison in MS Access.But I am trying to do the same file comparison using Shell script...Thats why posted you a thread..

I didn't understand this line in your script :
grep -v $line /tmp/b1.txt >/tmp/b2.txt 2>/dev/null

Can you please explain?

Thanks in advance
Regards
dave.....
Reply With Quote  
Join Date: Oct 2007
Posts: 22
Reputation: dave_nithis is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Question Re: Challenging Script

  #9  
Nov 19th, 2007
Originally Posted by eggi View Post
a's got 100
b's got 9
you need the 91 uniques - I'll put 'em in c.txt

while read line
do
   grep $line b.txt >/dev/null 2>&1               
   if [ $? -ne 0 ]
   then
       echo $line >>c.txt
   fi
done <a.txt

Sorry - just needed to drop a post somewhere - it's been a while

, Mike



Mike,

why are you using ">/dev/null 2>&1" in your script?
What condition you are checking in your "if" loop?

Regards
Dave
Last edited by dave_nithis : Nov 19th, 2007 at 7:57 am.
Reply With Quote  
Join Date: Feb 2006
Posts: 1,509
Reputation: masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light masijade is a glorious beacon of light 
Rep Power: 10
Solved Threads: 136
masijade's Avatar
masijade masijade is offline Offline
Posting Virtuoso

Re: Challenging Script

  #10  
Nov 19th, 2007
Originally Posted by dave_nithis View Post
Masijade,

I finished the file comparison in MS Access.But I am trying to do the same file comparison using Shell script...Thats why posted you a thread..

I didn't understand this line in your script :
grep -v $line /tmp/b1.txt >/tmp/b2.txt 2>/dev/null

Can you please explain?

Thanks in advance
Regards
dave.....


grep -v means to match everything except the next text (you should probably place $line in quotes) from the the first file listed (b1.txt) and output it to the second file listed ( > b2.txt) and ignore any error messages (2>/dev/null).

The next line then moves b2.txt to b1.txt so you can repeat the procedure with the next line. Once you have gone through all lines, b2.txt contains all lines from b that were not in a.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Shell Scripting Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Shell Scripting Forum

All times are GMT -4. The time now is 1:52 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC