The UNIX and Linux Forums  


Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
compare files danabo Shell Programming and Scripting 3 05-19-2008 01:09 PM
compare two files charandevu Shell Programming and Scripting 7 03-30-2008 04:20 PM
Compare files kharen11 UNIX for Advanced & Expert Users 25 03-14-2007 05:35 AM
Compare files Berserk Shell Programming and Scripting 6 09-05-2005 05:04 AM
compare files ingunix UNIX for Dummies Questions & Answers 3 05-24-2001 12:44 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 01-06-2009
Quijote Quijote is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 3
Compare between two files

Hello. For example, I have a list like this inside a text file called "list.txt"

list.txt
-----------------------------------------------------
01-01-2009 15:50 grapes 2$
01-01-2009 15:56 tea 4$
04-01-2009 20:30 oranges 1$
05-01-2009 17:20 carrots 3$
05-01-2009 17:34 milk 5$
-----------------------------------------------------


I have another text file, "products.txt", full of lines like this:

products.txt
------------------------
car
grapes
table
chair
oranges
------------------------
I'm looking for a Bash command able to compare "list.txt" with "products.txt" and if a word is found in both lists, as "grapes and oranges", then Bash could print another list like this:

---------------------------------------------------------
01-01-2009 15:50 grapes 2$
04-01-2009 20:30 oranges 1$
---------------------------------------------------------

I'm able to do that, but using a macro in Excel, but I would like to do it with GNU/LInux and give up Windows.

I have been trying with awk, sed, but I don't know how to do it.

Thanks and excuse my very bad knowledge about bash and about English language.
  #2 (permalink)  
Old 01-06-2009
jim mcnamara jim mcnamara is online now Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,787

Code:
grep -f products.txt  list.txt

make sure that products.txt does not have any trailing blank spaces.
  #3 (permalink)  
Old 01-06-2009
Quijote Quijote is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 3
Thank you very much for answering my question, dear jim mcnamara.

I'm afraid that code, grep -f, doesn't work very well because it confuses words like "car", located in the products.txt file, with words beginning with the letters car, like "carrots", located in the list.txt file.

Anyway I think that your code "grep -f" can facilitate my job, but I must brush up later all the files, by hand, to erase the possible mistakes written in the output list.

Again, thanks a lot for your answer.
  #4 (permalink)  
Old 01-06-2009
jaduks's Avatar
jaduks jaduks is offline
Registered User
  
 

Join Date: Aug 2007
Location: Assam,India
Posts: 167

Code:
$ awk 'NR==FNR{arr[$1];next}($3 in arr)' products.txt list.txt

  #5 (permalink)  
Old 01-06-2009
Quijote Quijote is offline
Registered User
  
 

Join Date: Jan 2009
Posts: 3
Great! Problem solved!

Thank you very much, dear jaduks.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 02:33 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0