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.

OS: Linux

link|improve this question
feedback

2 Answers

up vote 7 down vote accepted

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
link|improve this answer
1  
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 Zaslavsky Aug 25 '10 at 7:53
Thanks, nice to learn something by answering someone else's question. – Nathan O'Sullivan Aug 25 '10 at 8:08
feedback

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.

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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