LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 11-25-2005, 07:07 PM   #16
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware 14.1 (32 and 64 bit)
Posts: 1,594

Rep: Reputation: 125Reputation: 125

I just noticed the script fails on search phrases. I tried quoting "$regexp" with double quotes (") and upright single quotes (') in the script as well when giving it as argument to the script, but it suddenly gave much more results than it should. It looks as if the phrase was broken down to single words. Any ideas to solve this?
 
Old 11-26-2005, 04:52 AM   #17
titopoquito
Senior Member
 
Registered: Jul 2004
Location: Lower Rhine region, Germany
Distribution: Slackware 14.1 (32 and 64 bit)
Posts: 1,594

Rep: Reputation: 125Reputation: 125
Well, I got it working. My result in case anyone is interested follows. This script can handle long filename including spaces, search phrases (also including spaces, but quoted within doublequotes) and a not a priori defined number of search arguments.

Code:
#!/bin/sh
number_arguments="$#"
touch found-1
while test "$1" != "" ; do
        grep -i "$1" -l * >> found-1
        shift
done
sort found-1 > found-2
uniq -c found-2 | egrep "^[ ]*$number_arguments" | cut -c 9-
rm found-1
rm found-2
 
Old 12-11-2005, 07:01 AM   #18
Samsara
Member
 
Registered: May 2003
Distribution: Ubuntu, Mac OS X Tiger
Posts: 481

Rep: Reputation: 32
Just a few more general comments:
Code:
#!/bin/sh
number_arguments="$#"  # quotes not needed? (except for correct Vim colours?)

#touch found-1  # not needed
while test "$1" != "" ; do
        grep -i "$1" -l * >> found-1
        shift
done
sort found-1 | uniq -c | egrep "^[ ]*$number_arguments" | cut -c 9-
rm found-1
So we then have:
Code:
#!/bin/sh
number_arguments=$#

while test "$1" != "" ; do
        grep -i "$1" -l * >> found-1
        shift
done
sort found-1 | uniq -c | egrep "^[ ]*$number_arguments" | cut -c 9-
rm found-1
which you may find easier to read and maintain.

Samsara
.
 
Old 11-11-2010, 02:06 AM   #19
beng wooi
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Rep: Reputation: 0
ANDing in grep - search terms in different lines in files.

A 1 liner solution. No scripts.
- Supports file names with spaces
- Supports exp with spaces
- Supports multiple expressions.
- Should be efficient processing because xargs is used.

Code:
find * -print0 |xargs -0 grep -l 'exp 1' |xargs -i{} grep -l 'exp 2' "{}"|xargs -i{} grep -l 'exp 3' "{}"

Last edited by beng wooi; 11-12-2010 at 10:23 PM.
 
Old 11-15-2010, 05:12 AM   #20
steve-alexander
LQ Newbie
 
Registered: Mar 2005
Location: Ohio
Distribution: FC6->F7
Posts: 23

Rep: Reputation: 16
You have a bunch of $20 answers to a 50 cent question.

# cat > /tmp/foo < EOF
bash: EOF: No such file or directory
# cat > /tmp/foo << EOF
> cat
> dog
> fish
> rat
> EOF
#
# grep "cat\|fish" /tmp/foo
cat
fish
 
1 members found this post helpful.
Old 11-16-2010, 01:17 AM   #21
beng wooi
LQ Newbie
 
Registered: Nov 2010
Posts: 2

Rep: Reputation: 0
steve-alexander,

I think you are giving a OR solution. The original question asks to print out the file ONLY when exp1 AND exp2 AND exp3 AND exp4...and so on... exist in the file.

Last edited by beng wooi; 11-16-2010 at 01:18 AM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Regular expressions using grep linuxmandrake Programming 3 11-16-2005 05:29 PM
grep ?? can grep us variables? DaFrEQ Linux - Software 4 09-14-2005 01:22 PM
What does rpm -qa |grep th* (as compared to rpm -qa |grep th) display? davidas Linux - Newbie 2 03-18-2004 02:35 AM
"Undeleting" data using grep, but get "grep: memory exhausted" error SammyK Linux - Software 2 03-13-2004 04:11 PM
ps -ef|grep -v root|grep apache<<result maelstrombob Linux - Newbie 1 09-24-2003 12:38 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:12 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration