I want to delete a line if it contains a value that is specified.
2 5 8
1 3 7
8 5 9
So if I wanted to delete a line containing 7 as the third field:
{
if($3 == 7){
####delete the line
}
}
You can do this:
awk '$3 /7/ {next} {print}'
The other answers work. Here's why
Awk's standard processing model is to read a line of input, optionally match that line, and if matched (optionally) print the input. The other solutions use a negation match, so lines are printed unless the match is made.
Your code sample doesn't use a negation match: it says "if something is true, do it". Because you want to delete the input, when you match that target, you can just skip printing it.
{
if($3 == 7){
#skip printing this line
next
}
}
IHTH.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With