Awk 在匹配另一个IP之前,删除一个IP之后的匹配行

Awk 在匹配另一个IP之前,删除一个IP之后的匹配行,awk,Awk,如何在匹配另一个IP之前删除一个IP之后的匹配行 我在一个文本文件output.txt中获得了以下数据: 您可以看到,每个IP下面至少有一个域记录。每个IP下面必须至少有一个或多个域 我知道如何使用sed删除IP,如下所示,但我也不知道如何删除其域: ip="1.1.1.1" awk -v d=$ip '$0 != d' $output> tmp && mv tmp $output 这只会删除IP,但如何删除属于1.1.1.1的所有域(包括其IP)

如何在匹配另一个IP之前删除一个IP之后的匹配行

我在一个文本文件output.txt中获得了以下数据:

您可以看到,每个IP下面至少有一个域记录。每个IP下面必须至少有一个或多个域

我知道如何使用sed删除IP,如下所示,但我也不知道如何删除其域:

ip="1.1.1.1"
awk -v d=$ip  '$0 != d' $output> tmp && mv tmp $output
这只会删除IP,但如何删除属于1.1.1.1的所有域(包括其IP)

删除1.1.1.1后,新文件的预期输出如下:

1.2.3.4
okok.com
1.1.1.11
abc.com
kkk.net
1.4.4.4
abc.net
xyz.my
ttt.us
okok.com
1.8.3.33
jj.com
cpp.com
您可以使用此awk:

awk-vip=$ip'/^[0-9]+\.[0-9]+{3}$/{如果行与任何ip地址匹配 rem=$1==如果ip与输入匹配,则切换rem } !如果rem=0,则打印rem'ip.txt 您可以使用此awk:

awk-vip=$ip'/^[0-9]+\.[0-9]+{3}$/{如果行与任何ip地址匹配 rem=$1==如果ip与输入匹配,则切换rem } !如果rem=0,则打印rem'ip.txt 有了GNU awk,您可以尝试以下内容,并用显示的样本编写和测试

awk -v RS="([0-9]+\\\\.){3}[0-9]+" -v ip="1.1.1.1" '
prev{
  sub(/\n+$/,"")
  print prev $0
  prev=""
}
RT && RT!=ip{
  prev=RT
}'  Input_file
说明:增加对以上内容的详细说明

awk -v RS="([0-9]+\\\\.){3}[0-9]+" -v ip="1.1.1.1" ' ##Starting awk program.
##Setting RS as digit dot 3 times then digits here, creating variable ip with
##value which we do not want.
prev{                       ##Checking condition if prev is NOT NULL then do following.
  sub(/\n+$/,"")            ##Substituting new lines till end of line with NULL.  
  print prev $0             ##printing prev and current line here.
  prev=""                   ##Nullifying prev here.
}
RT && RT!=ip{               ##Checking condition if RT NOT NULL and RT not equal to ip
  prev=RT                   ##Then set prev to RT value here.
}'  Input_file              ##Mentioning Input_file name here.
有了GNU awk,您可以尝试以下内容,并用显示的样本编写和测试

awk -v RS="([0-9]+\\\\.){3}[0-9]+" -v ip="1.1.1.1" '
prev{
  sub(/\n+$/,"")
  print prev $0
  prev=""
}
RT && RT!=ip{
  prev=RT
}'  Input_file
说明:增加对以上内容的详细说明

awk -v RS="([0-9]+\\\\.){3}[0-9]+" -v ip="1.1.1.1" ' ##Starting awk program.
##Setting RS as digit dot 3 times then digits here, creating variable ip with
##value which we do not want.
prev{                       ##Checking condition if prev is NOT NULL then do following.
  sub(/\n+$/,"")            ##Substituting new lines till end of line with NULL.  
  print prev $0             ##printing prev and current line here.
  prev=""                   ##Nullifying prev here.
}
RT && RT!=ip{               ##Checking condition if RT NOT NULL and RT not equal to ip
  prev=RT                   ##Then set prev to RT value here.
}'  Input_file              ##Mentioning Input_file name here.

请添加您希望解决问题的标签。不要用不需要的标签发垃圾邮件。请添加你喜欢的标签来解决你的问题。不要向它发送不需要的垃圾邮件插入-i inplace in awk命令以将更改保存到文件中。-i inplace是GNU awk扩展名,不是吗?@rethab:是的,这是GNU awkfeature@anubhava是的,它在工作。。谢谢你对代码的解释,它帮助我了解了很多关于awk的写作风格。Insert-i inplace in awk命令将更改保存到文件中。-i inplace是GNU awk扩展名,不是吗?@rethab:是的,那就是GNU awkfeature@anubhava是的,它在工作。。谢谢你对代码的解释,这对我了解awk的写作风格有很大帮助。我想你需要在打印后添加prev=