[英]Delete all lines between two patterns (exclusive of the pattern) using sed or awk
[英]Can't read lines between two patterns using AWK
我正在尝试阅读 openvpn 状态日志以获取连接的用户。 我试过按照这里的答案,但没有运气。
这是我的文件:
OpenVPN CLIENT LIST
Updated,Mon Jul 13 10:53:46 2020
Common Name,Real Address,Bytes Received,Bytes Sent,Connected Since
user123,8.9.10.11:24142,143404433,5616022,Mon Jul 13 10:09:31 2020
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
192.168.1.2,user123,8.9.10.11:24142,Mon Jul 13 10:53:45 2020
GLOBAL STATS
Max bcast/mcast queue length,1
END
我想要“Common”和“ROUTING”之间的线条,例如:
user123,8.9.10.11:24142,143455713,5682214,Mon Jul 13 10:09:31 2020
使用这个:
awk '/Common/{flag=1;next}/ROUTING/{flag=0}flag' /pathtomylog/openvpn-status.log
我得到:
user123,8.9.10.11:24142,143455713,5682214,Mon Jul 13 10:09:31 2020
192.168.1.2,user123,8.9.10.11:24142,Mon Jul 13 11:00:36 2020
GLOBAL STATS
Max bcast/mcast queue length,1
END
任何帮助表示赞赏。
编辑:以下代码完美运行。 问题是Common
的第二个实例。
sudo awk '/ROUTING/{flag=""} /^Common/{flag=1;next} flag' Input file
您能否尝试使用 GNU awk
中的示例进行跟踪、编写和测试。
awk '/ROUTING/{flag=""} /^Common/{flag=1;next} flag' Input_file
为什么 OP 的代码不起作用:第一个标志由以Common
开头的行设置,然后再次由字符串Common
设置,后者也在ROUTING
之后,变量flag
再次设置,然后它永远不会取消设置,因为ROUTING
是在 2nd Common
之后没有找到,因此它从那里打印所有行。 因此,我更改了正在寻找/^Common/
的正则表达式,它与ROUTING
之后的其他行不匹配。
说明:为上述添加详细说明。
awk ' ##Starting awk program from here.
/ROUTING/{ ##Checking if a line starts from string ROUTING then do following.
flag="" ##Setting flag value to NULL here. Since we want to STOP printing from here onward.
}
/^Common/{ ##Checking condition if a line starting from Common then do following.
flag=1 ##Setting variable flag to 1 here.
next ##next keyword will skip all further statements from here.
}
flag ##Checking condition if flag is SET then print current line(since no action mentioned so by default printing of current line will happen) here.
' Input_file ##Mentioning Input_file name here.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.