简体   繁体   中英

How to delete a line in bash (awk) with 2 conditions

i ve the following problem: I have a file like:

1 3 4
2 5 6
3 1 3
4 1 0
5 7 0
6 0 1

and I would like to delete a line that contains in the second column 1 and in the third one the number 0. So the result should be:

1 3 4
2 5 6
3 1 3
5 7 0
6 0 1

I've tried with: awk '$2!=1 && $3 != 0' file

but it will delete also the lines: '5 7 0' and '3 1 3'

Any help?

You want:

awk '!( $2==1 && $3 == 0)' file

or, equivalently:

awk '$2 != 1 || $3' file

You're looking for the negation of $2 == 1 && $3 == 0 . By the rules of logic, that's

awk '$2 != 1 || $3 != 0'

This will ensure that:

 awk '!($2==1 && $3==0){print}' file

ie print all lines except those with 2nd column is 1 and 3rd is 0.

Try this

awk '$2$3!="10"' file

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM