我想删除一行,如果它包含指定的值.
2 5 8
1 3 7
8 5 9
Run Code Online (Sandbox Code Playgroud)
因此,如果我想删除包含7作为第三个字段的行:
{
if($3 == 7){
####delete the line
}
}
Run Code Online (Sandbox Code Playgroud)
删除包含7的行
awk '!/7/' yourFile
Run Code Online (Sandbox Code Playgroud)
其他答案有效.这就是原因
Awk的标准处理模型是读取一行输入,可选地匹配该行,如果匹配(可选)打印输入.其他解决方案使用否定匹配,因此除非进行匹配,否则将打印行.
您的代码示例不使用否定匹配:它表示"如果某些内容属实,请执行此操作".因为您要删除输入,所以当您匹配该目标时,您可以跳过它.
{
if($3 == 7){
#skip printing this line
next
}
}
Run Code Online (Sandbox Code Playgroud)
IHTH.