B3luT
B3luT

Reputation: 317

How to filter on column value in bash

I'm facing a problem while trying to grep (filter) a logfile on the value of an integer.

logfile.log:

2014-11-16 21:22:15 8 10.133.23.9 PROXIED ...
2014-11-16 21:22:15 1 163.104.40.133 authentication_failed DENIED ...
2014-11-16 21:22:15 15 163.104.40.134 authentication_failed DENIED ...
2014-11-16 21:22:16 9 163.104.124.209 PROXIED ...

I have an int in column 3 : 8, 1, 15, 9.

I need to grep only if: value > 10.

Something like:

cat logfile.log | grep {$2>10}
2014-11-16 21:22:15 15 163.104.40.134 authentication_failed DENIED ...

Upvotes: 5

Views: 16269

Answers (2)

Avinash Raj
Avinash Raj

Reputation: 174706

Through regex plus grep.

grep -P '^\S+\s+\S+\s+(?!0)(?:1[1-9]|[2-9]\d|\d{3,})\b' file

I assumed that there isn't a decimal number in the column 3.

DEMO

Upvotes: 0

fedorqui
fedorqui

Reputation: 289585

This should make!

$ awk '$3>10' file
2014-11-16 21:22:15 15 163.104.40.134 authentication_failed DENIED ...

With $3 we refer to the 3rd field, so that $3>10 means: lines having 3rd field bigger than 10. If this is accomplished, the condition is true and hence awk performs its default behaviour: print the line.

You could of course say: print just an specific field, which you could by saying awk '$3>10 {print $1}' file, for example.

Upvotes: 17

Related Questions