我已经在Combining two sed commands,https://unix.stackexchange.com/questions/407937/combine-two-sed-commands,combine two sed commands和其他问题中进行了阅读,但是仍然不知道如何组合两个sed命令。
这是我的示例文件(file.txt)。
10/8/18
6:54:42.000 PM
Oct 8 19:54:42 x.x.x.x 231 <134> 2018-10-08T18:54:42Z Server_Name: 2018-10-08 18:54:42 - Server_Name - [127.0.0.1] System()[] - User Accounts modified. Removed username JohnDoe from authentication server RSA.
host = Server_Name
source = /opt/x.x.x.x-20181008.log
sourcetype = sslvpn
10/8/18
6:47:33.000 PM
Oct 8 19:47:33 x.x.x.x 266 <134> 2018-10-08T18:47:33Z Server_Name: 2018-10-08 18:47:33 - Server_Name - [y.y.y.y] JohnDoe - Closed connection to z.z.z.z after 6547 seconds, with 5526448 bytes read and 15634007 bytes written
host = Server_Name
source = /opt/x.x.x.x-20181008.log
sourcetype = sslvpn
10/8/18
6:47:33.000 PM
Oct 8 19:47:33 x.x.x.x 229 <134> 2018-10-08T18:47:33Z Server_Name: 2018-10-08 18:47:33 - Server_Name - [y.y.y.y] JohnDoe - VPN Tunneling: Session ended for user with IPv4 address z.z.z.z
host = Server_Name
source = /opt/x.x.x.x-20181008.log
sourcetype = sslvpn
10/8/18
6:47:33.000 PM
Oct 8 19:47:33 x.x.x.x 204 <134> 2018-10-08T18:47:33Z Server_Name: 2018-10-08 18:47:33 - Server_Name - [y.y.y.y] JohnDoe - Logout from y.y.y.y (session:abc)
host = Server_Name
source = /opt/x.x.x.x-20181008.log
sourcetype = sslvpn
所需的输出
Oct 8 19:54:42 x.x.x.x 231 <134> 2018-10-08T18:54:42Z Server_Name: 2018-10-08 18:54:42 - Server_Name - [127.0.0.1] System()[] - User Accounts modified. Removed username JohnDoe from authe
ntication server RSA.
Oct 8 19:47:33 x.x.x.x 266 <134> 2018-10-08T18:47:33Z Server_Name: 2018-10-08 18:47:33 - Server_Name - [y.y.y.y] JohnDoe - Closed connection to z.z.z.z after 6547 seconds, with 5526448 by
tes read and 15634007 bytes written
Oct 8 19:47:33 x.x.x.x 229 <134> 2018-10-08T18:47:33Z Server_Name: 2018-10-08 18:47:33 - Server_Name - [y.y.y.y] JohnDoe - VPN Tunneling: Session ended for user with IPv4 address z.z.z.z
Oct 8 19:47:33 x.x.x.x 204 <134> 2018-10-08T18:47:33Z Server_Name: 2018-10-08 18:47:33 - Server_Name - [y.y.y.y] JohnDoe - Logout from y.y.y.y (session:abc)
我所做的(2条sed命令)产生输出的是
sed -n '/[0-9]\/[0-9]\//,+1!p' file.txt > file2.txt
sed -n '/host =/,+3!p' file2.txt
基于其他问题的答案,半冒号是解决方案,但我只是不确定如何使用它。这是我尝试使用完全无效的半冒号。
sed -n '/[0-9]\/[0-9]\//,+1!p;/host =/,+3!p' file.txt
请告知
答案 0 :(得分:2)
为什么不修改sed
使其仅匹配模式并删除而不定义否定条件?只需使用d
运算符
sed -e '/[0-9]\/[0-9]\//,+1d' -e '/host =/,+3d' file
我猜想您尝试使用-n
标志可能是无法将两个构造结合在一起的可能原因。因为根据定义,-n
标志将使sed
仅打印匹配的部分,而不打印其他所有行。
答案 1 :(得分:0)
您已得到问题的答案,但使用的工具错误。除了指定2个不同的否定条件(例如测试不需要的输出而不是执行的操作)以外,还有很多更好的方法可以执行所需的操作。例如:
grep -A 1 --no-group-separator '^[[:alpha:]]' file
awk '(NR%8) ~ /^[34]$/' file
awk '/^[[:alpha:]]/{c=2} c&&c--' file
或者如果您真的觉得需要使用sed:
sed -n '/^[[:alpha:]]/,/^/p' file
通常-避免使用负逻辑,因为它比正逻辑更难于理解(并且通常很难维护),并且会带来双重负逻辑的风险,这非常容易混淆且容易出错。