4

How can I filter some output only by certain column?

I need something like this:

tail -f logs/access_log | grep -v "200" --only-in-column=1

So I want to see only lines that don't have string '200' in first column.

15

awk is probably the canonical tool for this kind of problem.

$ cat data
foo 200 bar
foo 200 baz
bar 4   baz

$ cat data | awk '$2 != 200 { print $0 }'
bar 4   baz
  • 5
    By the way, you can write the action as { print } or even omit it entirely (just awk '$2 != 200'), since print $0 is the default action. – David Z Aug 25 '10 at 7:53
  • 1
    Thanks, nice to learn something by answering someone else's question. – Nathan O'Sullivan Aug 25 '10 at 8:08
0

This will work:

tail -f logs/access_log | grep -v '^200[[:blank:]]'

which excludes lines that begin with "200" followed by a space or a tab.

You can choose different delimiter sets depending on your needs.

tail -f logs/access_log | grep -v '^200[^[:alnum:]]'

which excludes lines that begin with "200" followed by any character other than alphabetic or numeric characters.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.