LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 03-22-2010, 05:51 AM   #1
moicpit
LQ Newbie
 
Registered: Mar 2010
Posts: 26

Rep: Reputation: 15
How to find multiple strings into multiple files ?


Hi !

I would like to find all the files that contains the strings I'm searching.

For example (it's just an example), I would like to search all the files in "/etc" that contains "eth0" and "us", whatever where are located those 2 strings, the important is that the 2 strings are in the files listed.

It would be something like a "grep -lr 'eth0' *" and "grep -lr 'us' *" but in one time/command, so that I don't have to make a comparison of the 2 list of files resulting from the 2 "grep" commands given higher.

Thank you !

Pit
 
Old 03-22-2010, 06:12 AM   #2
troop
Member
 
Registered: Feb 2010
Distribution: gentoo, arch, fedora, freebsd
Posts: 379

Rep: Reputation: 97
try
Code:
egrep -lr 'eth.*us|us.*eth' *

Last edited by troop; 03-22-2010 at 06:20 AM.
 
Old 03-22-2010, 07:20 AM   #3
moicpit
LQ Newbie
 
Registered: Mar 2010
Posts: 26

Original Poster
Rep: Reputation: 15
Thank you but it seems that it works just like the command following :
Quote:
grep -r "eth" * | grep "us"
Unfortunately, I don't want to search the files that have a line with "eth" and "us" but the files that have "eth" and "us" in it, whatever where the occurrences are.

Thank you again but it's not what I'm looking for.

I add that no matter if the solution is a BASH script. ;-)
 
Old 03-22-2010, 01:02 PM   #4
mario.almeida
Member
 
Registered: May 2008
Location: India
Distribution: Ubuntu 10.04, CentOS, Manjaro
Posts: 179

Rep: Reputation: 27
Hi,

Have you tried by reversing your search

egrep -l -v '(eth|us)' * | xargs egrep '(eth|us)'
 
Old 03-22-2010, 05:21 PM   #5
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
This uses 'grep -Z' with 'xargs -0' so that it can work with file names containing spaces.
Code:
grep -lZr 'eth0' * | xargs -0 grep -l 'us'
 
Old 03-23-2010, 03:32 AM   #6
moicpit
LQ Newbie
 
Registered: Mar 2010
Posts: 26

Original Poster
Rep: Reputation: 15
Thank you mario, this seems to work but Kenhelm's one is better : it does exactly what I want !

So thank you Kenhelm !

Is it possible to extend your solution in order to make a search on more than 2 strings ?

Thank you again !
Pit
 
Old 03-23-2010, 07:32 AM   #7
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
To search for more than two strings insert
| xargs -0 grep -lZ 'str'
into the middle of the pipeline for each extra string.
This is for three strings
Code:
grep -lZr 'eth0' * | xargs -0 grep -lZ 'str' | xargs -0 grep -l 'us'
 
Old 03-23-2010, 08:26 AM   #8
moicpit
LQ Newbie
 
Registered: Mar 2010
Posts: 26

Original Poster
Rep: Reputation: 15
... And if I understand well, this is for 4 strings :
Code:
grep -lZr 'str1' * | xargs -0 grep -lZ 'str2' | xargs -0 grep -lZ 'str3' | xargs -0 grep -l 'str4'
Thank you very much !
 
Old 04-22-2010, 04:18 AM   #9
moicpit
LQ Newbie
 
Registered: Mar 2010
Posts: 26

Original Poster
Rep: Reputation: 15
Hi !

I would like to search for files that have lines with 'eth0' but not 'eth1' in it.

Example :
Here is 3 files :
file1.txt :
Code:
line with eth0 and eth1
line with only eth0
file2.txt:
Code:
line with eth0 and eth1
file3.txt :
Code:
line with only eth0
Do you know what command can give me the following result :
Code:
file1.txt
file3.txt
Thanks !

-Pit
 
Old 04-23-2010, 11:05 PM   #10
Kenhelm
Member
 
Registered: Mar 2008
Location: N. W. England
Distribution: Mandriva
Posts: 360

Rep: Reputation: 170Reputation: 170
Try
Code:
for file in file1 file2 file3;do
  [ "$(sed -n '/eth0/{/eth1/! {=;q}}' "$file")" ] && echo "$file"
done
-n Suppresses normal sed output.
/eth0/{/eth1/! At any line containing eth0 but not eth1.
= Print the line number.
q Quit searching the file.

sed either prints a single line number or nothing, and that determines whether or not the file name is echoed.

This searches the current directory recursively.
It works with file names containing spaces.
Code:
while read file;do
  [ "$(sed -n '/eth0/{/eth1/! {=;q}}' "$file")" ] && echo "$file"
done < <(find . -type f -print)
 
Old 04-25-2010, 11:19 AM   #11
moicpit
LQ Newbie
 
Registered: Mar 2010
Posts: 26

Original Poster
Rep: Reputation: 15
OK, thanks, I'll try that ! ;-)
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
linux find to find files with multiple patterns subu_s Programming 6 12-15-2010 12:15 AM
Find & Replace string in multiple files Rudy Linux - General 14 04-15-2010 08:10 AM
Single find command to find multiple files? thok Linux - Newbie 7 01-31-2009 04:45 PM
Change text in multiple files in multiple directories vivo2341 Linux - General 5 11-27-2006 08:16 PM
find and copy files into multiple directories avargas22 Linux - Newbie 2 04-01-2004 11:11 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 01:29 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration