Challenging Script

Please support our Shell Scripting advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2007
Posts: 23
Reputation: dave_nithis is an unknown quantity at this point 
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Challenging Script

 
0
  #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.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,396
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 255
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Challenging Script

 
0
  #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 Quick reply to this message  
Join Date: Oct 2007
Posts: 23
Reputation: dave_nithis is an unknown quantity at this point 
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Re: Challenging Script

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

Re: Challenging Script

 
0
  #4
Nov 16th, 2007
Also am trying this in microsoft Access.
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,396
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 255
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Challenging Script

 
0
  #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 Quick reply to this message  
Join Date: Oct 2007
Posts: 399
Reputation: eggi will become famous soon enough eggi will become famous soon enough 
Solved Threads: 47
eggi eggi is offline Offline
Posting Whiz

Re: Challenging Script

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

Shell Scripting Syntax (Toggle Plain Text)
  1. while read line
  2. do
  3. grep $line b.txt >/dev/null 2>&1
  4. if [ $? -ne 0 ]
  5. then
  6. echo $line >>c.txt
  7. fi
  8. 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!
------------------------------------------------------------------------
The greatest viral marketing idea of all time, get your copy of this Free Report now!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,396
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 255
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Challenging Script

 
0
  #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

Shell Scripting Syntax (Toggle Plain Text)
  1. while read line
  2. do
  3. grep $line b.txt >/dev/null 2>&1
  4. if [ $? -ne 0 ]
  5. then
  6. echo $line >>c.txt
  7. fi
  8. 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.

Shell Scripting Syntax (Toggle Plain Text)
  1. cp b.txt /tmp/b1.txt
  2. while read line
  3. do
  4. grep -v $line /tmp/b1.txt >/tmp/b2.txt 2>/dev/null
  5. mv -f b2.txt b1.txt
  6. done <a.txt
  7. 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 Quick reply to this message  
Join Date: Oct 2007
Posts: 23
Reputation: dave_nithis is an unknown quantity at this point 
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Re: Challenging Script

 
0
  #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 Quick reply to this message  
Join Date: Oct 2007
Posts: 23
Reputation: dave_nithis is an unknown quantity at this point 
Solved Threads: 0
dave_nithis dave_nithis is offline Offline
Newbie Poster

Re: Challenging Script

 
0
  #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

Shell Scripting Syntax (Toggle Plain Text)
  1. while read line
  2. do
  3. grep $line b.txt >/dev/null 2>&1
  4. if [ $? -ne 0 ]
  5. then
  6. echo $line >>c.txt
  7. fi
  8. 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 Quick reply to this message  
Join Date: Feb 2006
Posts: 2,396
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 255
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Challenging Script

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC