删除包含相同模式的连续行

时间:2018-07-02 18:24:06

标签: sed grep

我想创建一个sed(或等效)表达式,该表达式将删除包含特定字符的连续行。例如,我有一个IP列表,后跟一个冒号。如果它们包含一个值,则以下行将不包含冒号。如果有连续的冒号行,则应删除第一行(因为它们为空),如下所示:

+159.0.0.0: 
+159.0.0.1: 
+443/tcp open https
+159.0.0.2: 
+159.0.0.3: 
+159.0.0.4: 
+159.0.0.5: 
+80/tcp open http
+443/tcp open https

所需结果:

+159.0.0.1: 
+443/tcp open https
+159.0.0.5: 
+80/tcp open http
+443/tcp open https

4 个答案:

答案 0 :(得分:2)

这可能对您有用(GNU sed):

sed 'N;/:.*\n.*:/!P;D' file

保持两行移动的窗口,并且如果两行都包含:,请不要打印第一行。

答案 1 :(得分:1)

编辑: 要检查最后一行是否带有冒号或是否也对代码进行了一些更改,如下所示。

awk '!/:/ && prev{print prev ORS $0;prev="";next} {prev=$0} END{if(prev && prev !~ /:/){print prev}}' Input_file


已对所提供的样品进行了全面测试,能否请您尝试按照以下步骤进行操作,并告诉我是否对您有帮助?

awk '!/:/ && prev{print prev ORS $0;prev="";next} {prev=$0} END{if(prev){print prev}}' Input_file

现在也添加一种非衬套形式的解决方案。

awk '
!/:/ && prev{
  print prev ORS $0;
  prev="";
  next
}
{
  prev=$0
}
END{
  if(prev){
    print prev}
}'  Input_file

说明: 现在也为上述代码添加了说明。

awk '
!/:/ && prev{          ##Checking condition here if a line is NOT having colon in it and variable prev is NOT NULL then do following.
  print prev ORS $0;   ##Printing the value of variable named prev ORS(whose default value is new line) and then current line by $0.
  prev="";             ##Nullifying prev variable value here.
  next                 ##Using awk out of the box next keyword which will skip all further statements from here.
}
{
  prev=$0              ##Setting value of variable prev to current line here.
}
END{                   ##Starting END section of current code here, which will be executed after Input_file is being read.
  if(prev){            ##Checking if variable prev is NOT NULL, if yes then do following.
    print prev}        ##Printing the value of variable prev here.
}' Input_file          ##Mentioning Input_file name here.

答案 2 :(得分:1)

另一个awk:

$ awk '/:/ { p = $0 } $0 !~ /:/ {if (p) {print p} print $0; p = ""} ' file
+159.0.0.1:
+443/tcp open https
+159.0.0.5:
+80/tcp open http
+443/tcp open https

答案 3 :(得分:1)

sed用于旧/新,全部。这将与任何UNIX盒子上的任何shell中的任何awk一起使用:

$ awk '/:/{s=$0 ORS;next} {print s $0; s=""}' file
+159.0.0.1:
+443/tcp open https
+159.0.0.5:
+80/tcp open http
+443/tcp open https

对于您可能想做的其他任何事情来说都是很简单的,例如处理以冒号结尾的最后一行,只需添加END部分以打印最后保存的冒号结尾行(如果有的话):

$ cat file
+159.0.0.0:
+159.0.0.1:
+443/tcp open https
+159.0.0.2:
+159.0.0.3:
+159.0.0.4:
+159.0.0.5:
+80/tcp open http
+443/tcp open https
+159.0.0.6:

$ awk '/:/{s=$0 ORS;next} {print s $0; s=""} END{printf "%s", s}' file
+159.0.0.1:
+443/tcp open https
+159.0.0.5:
+80/tcp open http
+443/tcp open https
+159.0.0.6: