user3108631
user3108631

Reputation: 91

filtering fields based on certain values

I wish you you all a very happy New Year.

I have a file that looks like this(example): There is no header and this file has about 10000 such rows

123 345 676 58 1 
464 222 0 0 1 
555 22 888 555 1 
777 333 676 0 1 
555 444 0 58 1 

PROBLEM: I only want those rows where both field 3 and 4 have a non zero value i.e. in the above example row 1 & row 3 should be included and rest should be excluded. How can I do this?

The output should look like this:

123 345 676 58 1 
555 22 888 555 1 

Thanks.

Upvotes: 0

Views: 43

Answers (1)

janos
janos

Reputation: 124704

awk is perfect for this kind of stuff:

awk '$3 && $4' input.txt

This will give you the output that you want.

$3 && $4 is a filter. $3 is the value of the 3rd field, $4 is the value of the forth. 0 values will be evaluated as false, anything else will be evaluated as true. If there can be negative values, than you need to write more precisely:

awk '$3 > 0 && $4 > 0' input.txt

Upvotes: 1

Related Questions