find files with 2 or more words


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting find files with 2 or more words
# 1  
Old 04-23-2008
find files with 2 or more words

I'm trying to find files that contain two or more specific words (e.g. all files with both "John" and "Mary" in them but not necessarily on the same line) but I don't know how to go about it. Maybe something like grep to find files with the first word, then grep the resulting set of files to find files with the second word, etc. However, I'm not sure how to pass the resulting list of files from the first grep to the next grep. Can someone give me a head start?
# 2  
Old 04-23-2008
Code:
grep -l John file more files | xargs grep -l Mary

grep -l prints the names of the files with matches. xargs arranges this list as command line arguments for the second grep; again, we only want grep to print the names of the matching files.

xargs is a general-purpose command for rearranging things so that the results from a pipeline can be passed as command-line arguments to another command.

A roughly equivalent construction would be to use backticks, but xargs has some additional niceties, such as being able to split the commands into multiple processes if the command line grows longer than the kernel can handle.

Here's the same with backticks (at the peril, then, of getting a "Command line too long" error):

Code:
grep -l Mary `grep -l John file more files`

Those are grave accents (ASCII 96), not regular apostrophes.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Find duplicate words using sed

I have following statement and I want to find duplicate word using sed command. How is it possible? "detect string and remove the duplicate string" There could be many statements in a file and each line may have duplicate word. Thanks! (1 Reply)
Discussion started by: jnrohit2k
1 Replies

2. Shell Programming and Scripting

Find common words

Hi, I have 10 files which needs to be print common words from those all files. Is there any command to find out. (2 Replies)
Discussion started by: munna_dude
2 Replies

3. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

4. Emergency UNIX and Linux Support

Find two words and join together in one file

Hi, I have a huge text file like below , I need to select only lines having line Fatal joined with id. like below i want the line to be Fatal Error for input record 25 is id = 543523. Waiting for your help. -----Original Message----- Acceptance with warnings for input record 24. 001 tag... (13 Replies)
Discussion started by: umapearl
13 Replies

5. Shell Programming and Scripting

Find out the words, just for fun

There is a spell game to find out the word which can be used between another two words, for example, play ______ table hair ______ ball So missing word will be: time (playtime, timetable) pin (hairpin, pinball) time and pin are also real word. Maybe there are not only... (7 Replies)
Discussion started by: rdcwayx
7 Replies

6. Shell Programming and Scripting

how to find number of words

please help me for this "divide the file into multiple files containing no more than 50 lines each and find the number of words of length less than 5 characters" (3 Replies)
Discussion started by: annapurna konga
3 Replies

7. UNIX for Dummies Questions & Answers

how to find common words and take them out from two files

Hi, everyone, Let's say, we have xxx.txt A 1 2 3 4 5 C 1 2 3 4 5 E 1 2 3 4 5 yyy.txt A 1 2 3 4 5 B 1 2 3 4 5 C 1 2 3 4 5 D 1 2 3 4 5 E 1 2 3 4 5 First I match the first column I find intersection (A,C, E), then I want to take those lines with ACE out from yyy.txt, like A 1... (11 Replies)
Discussion started by: kaixinsjtu
11 Replies

8. Shell Programming and Scripting

FInd the String between Two words

Hi I would like know how can write a script for find a string between two words. My input like this: a1 IN a1a1a1a1a1a1 OUT b1 IN b1b1b1b1b1b1 OUT c1 IN c1c1c1c1c1c1 OUT . . . now my out put like: a1a1a1a1a1a1 b1b1b1b1b1b1 c1c1c1c1c1c1 please help on this. (6 Replies)
Discussion started by: koti_rama
6 Replies

9. Shell Programming and Scripting

Script to find all the files that contain any of the words present in another file

Hi All, I am new to UNIX and shell scripts and also new to this forum. I need a script to find all the files in a directory that contain any of the strings present in another file. Please provide me the script or if you could provide pointers to any link in this forum it would be helpful.... (4 Replies)
Discussion started by: tsanthosh
4 Replies

10. Shell Programming and Scripting

find words with grep....

I have a .txt file which contains several lines of text. I need to write a script program using grep or any other unix tool so as to detect part of the text (words) between / / that begin with the symbol ~. For example if somewhere in the text appears a webpage address like... (8 Replies)
Discussion started by: chrisxgr
8 Replies
Login or Register to Ask a Question