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
  • 69,223
  • 56
  • 216
  • 327
solfish
  • 1,451
  • 3
  • 18
  • 23
  • 1
    Did you actually get `ls` to display a [mode string](https://askubuntu.com/questions/397493/what-does-the-first-character-of-unix-mode-string-indicate) starting with `w`? As far as I know `w` only appears in other positions in a mode string (where it means "writable"). *It's totally fine if you didn't and this is just a regex example.* But if it also represents a real situation then it might be valuable to have additional answers showing alternatives to [parsing the output of `ls`](http://mywiki.wooledge.org/ParsingLs), like using `find` with the `-perm` and `-name` tests and the `-ls` action. – Eliah Kagan Jul 29 '17 at 21:18
  • I don't think this is really a duplicate of [Grep searching two words in a line](https://askubuntu.com/questions/590384/grep-searching-two-words-in-a-line), which is about grepping for lines with two words *appearing anywhere, and in either order*. In this question, the line must begin in a specific way and end in a specific way. – Eliah Kagan Aug 02 '17 at 17:54

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
  • 54,268
  • 25
  • 157
  • 179
  • in this command ""egrep "^w.*\.pdf$" filename" meaning of ( \ ) is "and"? – solfish Jul 29 '17 at 20:28
  • No, \ is used to escape the dot (`.`), dot `.` means any char and `\.` means exactly a dot `.`. – Ravexina Jul 29 '17 at 20:29
  • first command better, i dont have any knowladge awk yet:) – solfish Jul 29 '17 at 20:36
  • 1
    Note that you can use `|` as a simple logical OR in grep extended regular expression (ERE) syntax (or, equivalently, `\|` in BRE) e.g. `egrep '(pat|mat)'` and `grep '\(pat\|mat\)'` are both equivalent to `grep -e pat -e mat` – steeldriver Jul 29 '17 at 20:41
  • @steeldriver Yeah, however that's a part of regex and `-e` is a `grep` functionality itself right? – Ravexina Jul 29 '17 at 20:44
  • @Ravexina do we need -e option to your last edit which belongs to @steeldriver? – solfish Jul 29 '17 at 20:51
  • @solfish yes, because `|` is a part of extended regular expression. `egrep '(bbb|ccc)'` works fine on your file however : `grep '(bbb|ccc)'` doesn't have any result. – Ravexina Jul 29 '17 at 20:53
  • thnanks @Ravexina could you make edit pls:)) – solfish Jul 29 '17 at 20:54