1

Let's say I have a file which has some ls -l output:

waaa- foo.pdf
-bbb- foobar.pdf
-ccc- foobar
waaa- foobar

I'd like to get just the first line

waaa -foo.pdf

as the final result, and I'm trying:

egrep -E "^w" .file | egrep -E "*.pdf"

Is there any way to combine these two searches?

Zanna
  • 72,312
solfish
  • 1,591

1 Answers1

5

You have to write it like:

egrep "^w.*\.pdf$" filename
  • Means started with w followed by any character and ended to .pdf.

for a logical "or" you can use -e switch:

egrep -e pattern1 -e pattern2

means all lines with pattern1 or pattern2.

or as @steeldriver suggested, use extended regex "or":

egrep "(pattern1|pattern2)"

and as you know for extended regular expressions you have to use egrep and not grep, e.g:

egrep '(bbb|ccc)' # works fine for your file
grep '(bbb|ccc)' # doens't have any result

For an "and" you have to pipe it to another egrep:

grep pattern1 | grep pattern2

means all lines with both pattern1 and pattern2.

or use another tools like awk:

awk '/pattern1/ && /pattern2/` filename
Ravexina
  • 57,256