views:

43

answers:

3

hello everyone, can You explain please, can grep pick rows if at least one element from the list appeared, for exmaple

grep "hello world" file1 

grep must give me all rows which have or word hello or world or both of them, thanks in advance

A: 

how about

grep -r "hello\|world" file1

that's a recursive grep by the way. it searches recursively for the term "hello world" in file1. it can also apply to a directory like so:

grep -r "hello\|world" dir/dir2/

corroded
no, doesn't work:<
lego69
I need also cases when appears only hello or only world
lego69
you can use the above grep by Anton but just add the -r for recursive. something like grep -r "hello\|world" file1
corroded
+3  A: 
grep "hello\|world" file1
Anton
sorry, but I have something like "$numbers" not exactly "hello world", so it will not work, any other ideas?
lego69
@lego69: numbers="1 3 5" Somtething like that?
Anton
yes, exactly and now I want to choose all rows which contain these numbers
lego69
or_nums=$(echo "$numbers" | sed -e 's: :\\|:g')grep "$or_nums" file
Anton
@Anton; you need a semi-colon before the grep (in your comment, not your answer). Without the semi-colon, or_nums is being set in grep's environment, but the shell is not expanding $or_nums in the argument list to grep.
William Pursell
@William Pursell: Well actually I had a newline - but I couldn't figure out how to preserve it in the comment and format the block as code :(
Anton
A: 

put your patterns in some file patterns.txt, one pattern per line, and run

grep -Ff patterns.txt file1
zed_0xff