【问题标题】:How to combine two sed commands?如何组合两个 sed 命令?
【发布时间】:2018-10-09 21:43:39
【问题描述】:

我在Combining two sed commandshttps://unix.stackexchange.com/questions/407937/combine-two-sed-commandscombine two sed commands 和其他问题中一直在阅读有关此内容的信息,但仍然不知道如何组合 2 个 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

请指教

【问题讨论】:

    标签: sed


    【解决方案1】:

    为什么不修改您的sed 以匹配模式并删除而不是定义否定条件?只需使用d 运算符删除行

    sed -e '/[0-9]\/[0-9]\//,+1d' -e '/host =/,+3d' file
    

    我敢打赌,您尝试中的-n 标志可能是无法将这两个构造组合在一起的可能原因。因为根据定义,-n 标志会让sed 只打印匹配的部分,而不是每隔一行打印。

    【讨论】:

    • 哇!太棒了。使用实际的日志文件对其进行了测试,并且可以正常工作。无论如何,我仍然想知道如何组合前面的 2 个 sed 命令.. 只是为了学习目的。谢谢
    • @Sabrina 你需要sed -n '/[0-9]\/[0-9]\//,+1{!p;b};/host =/,+3!p' 这样第一个命令不会影响以后的条件检查。正如Inian 所说,删除是更好的选择......另外,请参阅stackoverflow.com/questions/5864146/use-slashes-in-sed-replace
    【解决方案2】:

    您得到了问题的答案,但您以错误的方式使用了错误的工具。有很多比指定 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
    

    一般情况下 - 避免负面逻辑,因为它比正面逻辑更难理解(并且通常维护),并且引入了双重否定逻辑的风险,这非常令人困惑和容易出错。

    【讨论】:

      猜你喜欢
      • 2011-12-01
      • 2016-07-31
      • 2015-01-30
      • 1970-01-01
      • 2012-06-05
      • 1970-01-01
      • 2014-08-21
      • 2012-09-02
      • 2017-01-01
      相关资源
      最近更新 更多