The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com



UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Finding Names in multiple files Rally_Point UNIX for Dummies Questions & Answers 3 02-23-2009 08:36 PM
Finding missing sequential file names Julolidine Shell Programming and Scripting 2 07-02-2008 11:52 PM
Finding files with names that have a real number greater then difined. harmonwood Shell Programming and Scripting 2 11-09-2007 10:28 AM
finding duplicate files by size and finding pattern matching and its count jerome Sukumar Shell Programming and Scripting 2 12-01-2006 04:20 AM
how to find capital letter names in a file without finding words at start of sentence kev269 Shell Programming and Scripting 1 04-10-2006 09:35 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-12-2009
Rally_Point Rally_Point is offline
Registered User
  
 

Join Date: Dec 2008
Posts: 11
Finding names in multiple files - second attempt

I couldn't find the original thread that I created and since I didn't get a definitive answer, I figured I'd try again. Maybe this time I can describe what I want a little better.


I've got two files, each with thousands of names all separated by new line. I want to know if 'name in file1' also appears anywhere in file 2 then print it out for me (though output isn't the problem).

I've thought that maybe there's a way to walk through the first file and with each line then walk through each line in the second file (something like a nested loop statement) and then go from there. I'm just wondering if that will work of if there is a better and EASIER way.

Thanks
  #2 (permalink)  
Old 03-12-2009
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
  
 

Join Date: Feb 2004
Location: NM
Posts: 5,643
Use associative arrays in awk. This finds names that occur in both files. It is case-sensitive and sensitive to extra spaces - in other words an exact match
Code:
awl '{ if(FILENAME=="file1") (arr[$0]++}
        if(FILENAME=="file2") {if($0 in arr) {print $0)} }
      }'  file1 file2
Nested loops like you describe take far too long. This is about as godd as it gets.
  #3 (permalink)  
Old 03-12-2009
Lakris Lakris is offline
Registered User
  
 

Join Date: Oct 2007
Posts: 240
Quote:
Originally Posted by Rally_Point View Post
I've thought that maybe there's a way to walk through the first file and with each line then walk through each line in the second file (something like a nested loop statement) and then go from there.
Thanks
Hi, have You tried this?

Something like:

Code:
while read name;do grep "$name" file2;done < file1
And You could do something like:

Code:
while read name;do echo $name:;grep "$name" file2;done < file1
Or some other action based on return value of grep?

Code:
while read name;do ;grep -q "$name" file2 && echo $name found in file2;done < file1
And it would help if You could show the source of the file, if this doesn't work. Have You considered sorting the files and using diff?

Best regards
/Lakris
Sponsored Links
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 08:44 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language translation by Google.
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