我有一个大文件,有一些常规模式
snaps1: Counter: 4966
Opens: Counter: 357283
Instance: s.1.aps.userDatabase.mount275668.attributes
snaps1: Counter: 0
Opens: Counter: 357283
Instance: s.1.aps.userDatabase.test.attributes
这些线在上下的其他线之间重复。我需要打印snaps1行并获取实例:line所以我需要搜索snaps1但是只有当counter大于0然后打印snaps1行以及实例行时才会这样。
本网站的某位提供了答案
awk '/snaps1/{s=$0; c=$NF} /Instance/ && c{print s ORS $0}' file
这有效,但我不确定它是如何工作的。我还需要知道这是否摆脱了空间?如果我想比较一个文本而不是上面的零,该怎么办?所以不是寻找零,我将寻找snap1,它只等于ZZZ,我想在比较snap1>时打印包含实例的行作为上面提供的答案。零。有人可以回复,请提供有关如何工作的详细信息,以便我可以在其他地方使用它?
snaps1: Counter: ZZZ
Opens: Counter: 357283
Instance: s.1.aps.userDatabase.test.attributes
snaps1: Counter: ABC
Opens: Counter: 357283
Instance: s.1.aps.userDatabase.test.attributes
所以从上面的行我应该看到这个输出
snaps1: Counter: ZZZ
Instance: s.1.aps.userDatabase.test.attributes
感谢您提供的任何帮助
答案 0 :(得分:0)
你可以像这样使用它:
awk '/snaps1/ && $NF=="ZZZ"{s=$0} /Instance/ && s{print s ORS $0; s=""}' file
snaps1: Counter: ZZZ
Instance: s.1.aps.userDatabase.test.attributes
Awk命令细分:
/snaps1/ && $NF=="ZZZ" # if line has "snaps1" and last field is "ZZZ"
{s=$0} # store current line in variable s
/Instance/ && s # if line has "Instance" and variable s is not empty
{print s ORS $0; s=""} # print variable s ORS and current line. Reset variable s