[quote=xubuntu56;303031161]
I know this works:
ls | grep ^p | grep x$ | grep a
but I'm thinking there must be a way to do it without typing grep three times. Some of my attempts:
You are right and there is - in fact there are two ways:
First, you can use several expressions in grep at once by using the -e-switch:
grep -e "one" -e "two" /some/file
will list all lines containing "one" AND all lines containing "two" from that file. It is in fact a logical OR for these two expressions.
The second possibility (and this is probably what you wanted) is to combine regular expressions. i.e. to get all lines containing "one" and "two" in that order you would write:
grep 'one.*two' /some/file
The regexp means: "one", followed by anything (".*"), followed by "two". If the order of the two words should not matter you need two regexps, which you can combine with the method above:
grep -e 'one.*two' -e 'two.*one' /some/file
Search for "'one', something, then 'two'" or for "'two', something, then "one"'.
You can also use sed
(the stream editor) for such (or even more complex) purposes where grep
might get a bit unwieldy: sed -n
will only print the lines you explicitly print (the default is to print every line after it is processed, which includes passing it through unchanged) therefore:
sed -n '/one/p' /some/file
basically is the same as
grep 'one' /some/file
But you can nest certain rules in sed
which grep
cannot do. i.e.
sed -n '/one/ {
/two/ {
/three/p
}
}' /some/file
Would be similar to the example above: print only the lines containing "one", "two" and "three" but in any order.
I hope this helps.
bakunin