2

I would like to search for lines that contain "uploaded" but do not contain "09"

Is there a way to do this with grep?

(CentOS 5.6 if it matters).

user9517
  • 115,471
  • 20
  • 215
  • 297
JT White
  • 185
  • 1
  • 1
  • 6

4 Answers4

8

I usually chain greps to do this.

grep uploaded $file | grep -v 09
BillThor
  • 27,737
  • 3
  • 37
  • 69
4

You can use the -v option to grep to invert the match so

grep uploaded file | grep -v 09

will do what you want. This finds the lines that contain uploaded which are passed piped into a grep command to ignore lines with 09 in them.

user9517
  • 115,471
  • 20
  • 215
  • 297
4

This isn't using grep - but anytime I have a need that requires more than a basic grep, I turn to my favorite, sed. Certainly any time I have to chain grep commands together...

Use this command to do it:

sed -n '/09/d; /uploaded/p' file

Just one single command (not two).

Mei
  • 4,590
  • 8
  • 45
  • 53
2

Try simply:

( grep -v 09 | grep uploaded ) < file

Example:

$ cat file
1 uploaded 09
2 09
3 uploaded
4 text
$ ( grep -v 09 | grep uploaded ) < file
3 uploaded