繁体   English   中英

如何仅在下一行不匹配时打印行使用 sed 或 awk

[英]How to print lines only if next line is not matching using sed or awk

我在日志文件中有以下模式,其中“开始”在下一行以“结束”结束。 我希望仅在下一行模式与“结束”不匹配时才打印,即打印所有未结束的开始。

$ egrep AccountResource testlog.txt|egrep "Starts|Ends"
05:20:34.949 INFO  c.b.h.r.rest.Account - AccountResource for account:12345 - Starts
05:20:45.863 INFO  c.b.h.r.rest.Account - AccountResource () - Ends
05:20:46.274 INFO  c.b.h.r.rest.Account - AccountResource for account:12345 - Starts
05:20:46.360 INFO  c.b.h.r.rest.Account - AccountResource () - Ends
05:22:21.703 INFO  c.b.h.r.rest.Account - AccountResource for account:12345 - Starts
05:22:22.680 INFO  c.b.h.r.rest.Account - AccountResource for account:5678 - Starts
05:52:48.578 INFO  c.b.h.r.rest.Account - AccountResource for account:5678 - Starts
05:52:50.673 INFO  c.b.h.r.rest.Account - AccountResource () - Ends
05:52:50.937 INFO  c.b.h.r.rest.Account - AccountResource for account:12345 - Starts
05:52:50.977 INFO  c.b.h.r.rest.Account - AccountResource () - Ends
06:09:35.951 INFO  c.b.h.r.rest.Account - AccountResource for account:5678 - Starts
06:09:36.409 INFO  c.b.h.r.rest.Account - AccountResource () - Ends
06:09:36.690 INFO  c.b.h.r.rest.Account - AccountResource for account:5678 - Starts
06:09:36.720 INFO  c.b.h.r.rest.Account - AccountResource () - Ends

以下预计为 output

05:22:21.703 INFO  c.b.h.r.rest.Account - AccountResource for account:12345 - Starts
05:22:22.680 INFO  c.b.h.r.rest.Account - AccountResource for account:5678 - Starts

您可以使用一个awk来完成这项工作:

awk '!/AccountResource/{next} NR == nl && $NF!="Ends" {print p} $NF=="Starts" {p=$0; nl=NR+1}' testlog.txt

05:22:21.703 INFO  c.b.h.r.rest.Account - AccountResource for account:12345 - Starts
05:22:22.680 INFO  c.b.h.r.rest.Account - AccountResource for account:5678 - Starts

更具可读性的版本:

awk '
!/AccountResource/ {next}
NR == nl && $NF != "Ends" {print p}
$NF == "Starts" {
   p = $0
   nl = NR+1
}' testlog.txt

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

sed 'N;/Starts\n.*Ends$/d;P;D' file

一次处理两行。

如果第一个以Starts结尾,第二个以Ends ,则删除两者。

否则打印/删除第一个并重复。

使用您显示的示例,请尝试以下awk代码。

awk '
!/AccountResource/{ next      }
/AccountResource.*Starts$/{
  if(found && val){ print val }
  found=1
  val=$0
}
/AccountResource.*Ends$/{
  found=0
  val=""
}
'  Input_file

说明:为上文添加详细说明。

awk '                              ##Starting awk program from here.
!/AccountResource/{ next      }    ##If a line does not contain AccountResource move cursor to next line.
/AccountResource.*Starts$/{        ##Checking condition if line contains AccountResource and ends with Starts then do following.
  if(found && val){ print val }    ##If found and val both are SET then print val.
  found=1                          ##Setting found to 1 here.
  val=$0                           ##Setting val to current line here.
}
/AccountResource.*Ends$/{          ##Checking condition if line contains AccountResource and ends with Ends then do following.
  found=0                          ##Set found to 0 here.
  val=""                           ##Nullify val here.
}
'  Input_file                      ##Mentioning Input_file name here.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM