ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is there a command out there that will search for multiple patterns in each line of a file?
what i mean by that is....
if i was to search a file for the patterns "Hello" and "Goodbye", is there a command out there that will bring up all the lines in that file that contain only "Hello" AND "Goodbye" on the same line (not every line which has either Hello or Goodbye or both on it).
i would have thought grep would have been able to do this, but all i could get it to do was search "Hello " or "Goodbye", not "Hello "and "Goodbye"
Does anybody know a command that will do this for bash?
what if there were multiple instances of the pattern though.. not just 2.
say if a user were to enter 3 serch items in at the command line? and perhaps next time 5, and then 1 etc etc.
i need to write a shells script that will search a file for multiple patterns (that are passed to it through the command line), so i was wondering, if possible, is there a program out there that would take a string of patterns (each separated by a space, or some delimitering character), and then search for each pattern in the string?
if not, i think ill have to rethink my program design (-:
You can try to use regular expressions and grep: "grep -e" or plain "egrep" but that would get messy and complicated in a hurry.
Or, since you said you're doing this in a shell script, do this (pseudo-code of course):
Code:
if temp_file exists
remove it
copy file from command line to temp_file
foreach pattern on command-line
begin
cat temp_file | grep argument > temp_file2
mv temp_file2 temp_file
end
cat temp_file
Hmm.. looks good.. do u think that it would a reasonably efficent script? will the use of the extra temp files, and the moving of those files suck a bit of time?
yer.. unfortunatly in this case i have to stick to pure shell.. (i havent delt with awk, or pearl before, i have heard, and i am asuming they are different languages)
cheers!
#!/bin/bash
original_filename=<filename argument from command-line>
start_pattern=<first_pattern on command-line>
command_to_exec="grep -e \"$start_pattern\" $original_filename"
if( number of patterns on command-line > 1 )
begin
foreach remaining arg on command-line
begin
command_to_exec="$command_to_exec | grep -e \"$arg\" "
end
end
$command_to_exec
EDIT:
Added double quotes around the string manipulation... not sure if they're needed though... And removed the stupid command_base variable... not needed in the slightest...
Last edited by Dark_Helmet; 09-03-2003 at 10:40 PM.
Dark_Helmet: I was about to dock you one point for gratuitous piping of cat to grep (since grep takes a filename), but then you fixed it .. so you're in the clear for now
/me leers at Dark_Helmet as he walks out of the room
In fact, it reminds me of an equally embarrassing scene from the movie from which you derive your namesake (I presume) ... "Did you see anything??!!?" "NO! I DIDN'T SEE YOU PLAYING WITH YOUR DOLLS, SIR!!" "GOOD!"
ok.. ive tried wring the code that you suggested at the top.. but i cant seem to get it working
if i type in
grep -e $PATTERN1 $FILENAME | grep -e $PATTERN2
into the shell prompt, it works fine.. BUT! as soon as i try store it into a shell variable, for some reason the pipe character ( | ) looses it's meaning??
The above code shows everything that matches the first grep statement (for PATTERN1), but the second one (PATTERN2) is forgoton.. why does this happen?? its making me very sad ) :
lol.. n e way.. if u could help out, and soon.. that would be much appreciated.
Not to hijack the thread, but YES! Dark Helmet is my personal hero. Powerful. confident, and very focused on appearance. I mean, can you really be a menacing presence without an appropriate tie??
"Yogurt! I HATE Yogurt! Even with strawberries..."
If you don't need to search using real regular expressions, but only litteral strings, you could try this pure bash script. No grep, no temp files. Not as fast as "grep ... | grep ... | grep ..." though..
Give as many strings you like as arguments on the command line for this script, and it will print only the lines of the file "data.txt" containing all the strings given on the command line.
Code:
#!/bin/bash
FILE=data.txt
cat $FILE | while read LINE ; do
match=1
for WORD in $* ; do
tmp=${LINE%%${WORD}*}
if [ "${#LINE}" == "${#tmp}" ] ; then
match=0
break
fi
done
test "$match" == "1" && echo "$LINE"
done
If you name the script as "showlines", use it like this:
./showlines 'filetobesearched' 'search phrase'
(note that the parameters must be quoted)
Naturally, 'filetobesearched' may denote multiple files, like '*.htm', and there can be any number of words in 'search phrase' (there will be as many grep commands executed in a chain as many words there are in the search phrase)
The weakness of the script: in its present state there should be no leading or trailing spaces in the search phrase, and the words in the search phrase should be separated by exactly one space or an error occurs. However, it seems to work fine with these restrictions.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.