Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 10 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
share|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 Z 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

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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