Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Awk delete line that contains data

Tags:

awk

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
}
}
like image 682
user1017243 Avatar asked Oct 27 '11 21:10

user1017243


2 Answers

You can do this:

awk '$3  /7/ {next} {print}'
like image 152
mr.tee Avatar answered Oct 23 '22 04:10

mr.tee


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.

like image 29
shellter Avatar answered Oct 23 '22 05:10

shellter