sed - 如何组合两个 sed 命令?
问题描述
我在Combining two sed commands,https://unix.stackexchange.com/questions/407937/combine-two-sed-commands,combining 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
的匹配模式并删除而不是定义否定条件?d
只需使用运算符删除行
sed -e '/[0-9]\/[0-9]\//,+1d' -e '/host =/,+3d' file
我敢打赌-n
,您尝试中的标志可能是无法将这两个构造结合在一起的可能原因。因为根据定义,该-n
标志将只打印匹配sed
的部分,而不是每隔一行打印。
推荐阅读
- soap - 使用 Office 365 SOAP 的 OAuth 返回“受众声明值无效”
- c# - 如何在 WPF 中为 Windows 屏幕保护程序编写文件
- r - 是否可以为 blogdown“新帖子”插件提供多个 Rmd 文件模板?
- python - 读取数据文件(鲍鱼)并转换为 numpy 数组
- java - 如何从 JVisualVM 导出 MBean 数据/统计信息
- angular - 单击删除文本选择时的 Angular HostListener
- docker - 消费者没有收到来自 Kafka 的消息
- amazon-web-services - AppSync 批量更新项目?
- svg - SVG 在浏览器中生成白线
- java - 将值返回到另一个类并存储