Finding names in multiple files - second attempt


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Finding names in multiple files - second attempt
# 1  
Old 03-12-2009
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  
Old 03-12-2009
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  
Old 03-12-2009
Quote:
Originally Posted by Rally_Point
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
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding a string in a list of files, print file names

I'm interested in writing a report script using BASH that searches all of the files in a particular directory for a keyword and printing a list of files containing this string... In fact this reporting script would have searches for multiple keywords, so I'm interested in making multiple... (2 Replies)
Discussion started by: chemscripter904
2 Replies

2. Shell Programming and Scripting

Finding files in directory with similar names

So, I have a directory tree that has many files named thusly: X_REVY.PDF I need to find any files that have the same X portion (which can be nearly anything) as any another file (in any directory) but have different Y portions (which can be any number from 1-99). I then need it to return... (3 Replies)
Discussion started by: Kamezero
3 Replies

3. Shell Programming and Scripting

Finding size of files with spaces in their file names

I am running a UNIX script to get unused files and their sizes from the server. The issue is arising due to the spaces present in the filename/folder names.Due to this the du -k command doesn't work properly.But I need to calculate the size of all files including the ones which have spaces in them.... (4 Replies)
Discussion started by: INNSAV1
4 Replies

4. UNIX for Dummies Questions & Answers

finding overlapping names in different txt files

Dear Gurus, I have 57 tab-delimited different text files, each one containing entries in 3 columns. The first column in each file contains names of objects. Some names are present in more than one file. I would like to find those names and store them in a separate text file, preferably with a... (6 Replies)
Discussion started by: Unilearn
6 Replies

5. Shell Programming and Scripting

editing names of files in multiple folder

I have 1000's of directories which is named as numbers. Each directory contains multiple files. Each of these directories have a file named "att". I need to rename all the att files by adding the directory name followed by "_" then att for each of the directories. Directories 120 att... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

6. Shell Programming and Scripting

finding multiple files using find command

I am trying to search for 2 files using the find command as below find -name file1.txt -a -name file2.txt It doesn't give a result although the files exist in the folder, however when i try the following find -name file1.txt -o -name file2.txt It does give me the result. ./file2.txt... (4 Replies)
Discussion started by: vivek_damodaran
4 Replies

7. UNIX for Dummies Questions & Answers

Finding nth line across multiple files

I have several files (around 50) that have the similar format. I need to extract the 5th line from every file and output that into a text file. So far, I have been able to figure out how to do it for a single file: $ awk 'NR==5' text1.txt > results.txt OR $ sed -n '5p' text1.txt > results.txt... (6 Replies)
Discussion started by: oriqin
6 Replies

8. Shell Programming and Scripting

multiple regex finding in files

Hello folks, I have a text file aa.txt that contains below text (\')|(\-\-) ((\%3D)|(=)) 20%0d% i want to search each line pattern in /opt/1.log and /opt/2.log. Can some one suggest (1 Reply)
Discussion started by: learnbash
1 Replies

9. UNIX for Dummies Questions & Answers

Finding Names in multiple files

What's the best way to see if a common name exists in two separate files? (3 Replies)
Discussion started by: Rally_Point
3 Replies

10. Shell Programming and Scripting

Finding files with names that have a real number greater then difined.

I am trying to find all files in a directory whose name has a real number larger then the number I am looking for. For example: . |-- delta.1.5.sql |-- delta.2.1.sql |-- delta.2.2.sql |-- delta.2.3.sql |-- delta.2.4.sql `-- delta.2.5.sql I know my database is at 2.2 so I want an... (2 Replies)
Discussion started by: harmonwood
2 Replies
Login or Register to Ask a Question