Finding Names in multiple files


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Finding Names in multiple files
# 1  
Old 02-23-2009
Finding Names in multiple files

What's the best way to see if a common name exists in two separate files?
# 2  
Old 02-23-2009
Grep can accept multiple filenames as arguments. Suppose you're looking in file1, file2 and file3 for the name treesloth. You can simply do:

Code:
grep treesloth file1 file2 file3

The system should assume, unless you use switches to modify it, that the first argument is the regular expression you're looking for, and all additional arguments are files to search in.
# 3  
Old 02-23-2009
Thanks, hey! I've been to Orem, Utah. There is a Thai restaurant there that is absolutely great!

Anyway, back to my question. I need a script that can find names in multiple files but I don't know the name up front. I just want to know

if name

exists in file1 AND in file 2

then

echo "name"

I know I could use grep if I knew the name in advance, but I don't.

Thanks for the help.
# 4  
Old 02-23-2009
I'm not completely sure what you mean when you say that you "don't know the name up front". Do you mean that you just don't happen to know right now what you'll be searching for, but when the time comes you'll have a definite search term? If so, you could do something like:

Code:
foreach term ( searchterm1 searchterm2 searchterm3 )
echo -n $term "  " ; grep -l $term file1 file2 file3 | wc -l
end

That will show, for each search term, how many different files it appears in. Alternately, this:

Code:
foreach term ( searchterm1 searchterm2 searchterm3 )
echo -n $term "  " ; grep -l $term file1 file2 file3 | tr '\n' ' ' ; echo " "
end

However, I suspect that this comes closest to what you need:

Code:
foreach term ( searchterm1 searchterm2 searchterm3 )
set filecount = `grep -l $term file1 file2 file3 | wc -l`
if ( $filecount > 1 ) then
echo $term $filecount
endif
end

That will give you the search term, followed by the number of files it appears in, so long as that number is 2 or more. You can make it just show the search term by removing the "$filecount" from the 4th line. That sounds close to what you describe. If not, let us know and I'm sure we can whip it into shape.

Oh, this is all in CSH.

Sadly, I haven't had a chance to check out the Thai food around here yet. It's on the list, but if you recall the name, I'll bump it to the top. Have fun.
 
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

finding matches between multiple files from different directories

Hi all... Can somebody pls help me with this... I have a directory (dir1) which has many subdirectories(vr001,vr002,vr003..) with each subdir containing similar text file(say ras.txt). I have another directory(dir2) which has again got some subdir(vr001c,vr002c,vr003c..) with each subdir... (0 Replies)
Discussion started by: bramya07
0 Replies

6. 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

7. 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

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 - 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'... (2 Replies)
Discussion started by: Rally_Point
2 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