简体   繁体   English

Unix:使用sed删除每行的一部分

[英]Unix: Using sed remove part of each line

I am trying to remove the Who: ,What: ,When: ,Where: and the proceeding space from each line in a text file. 我正在尝试从文本文件的每一行中删除Who:,WhatWhat,When时,Where以及行进空间。 Below is a example: 下面是一个示例:

Who: Tester1+Password
What: Authentication Success
When: Tues March 20, 2015 08:15:02 UTD
Where: 198.192.1.2

If you want to remove anything upto first : , then you can do: 如果要删除第一个: ,则可以执行以下操作:

sed -re 's/(^[^:]+: )(.*)/\2/' file
Tester1+Password
Authentication Success
Tues March 20, 2015 08:15:02 UTD
198.192.1.2

As Glenn suggested we can avoid capture groups completely by removing the portion we don't need. 正如Glenn所建议的,我们可以通过删除不需要的部分来完全避免捕获组。

sed 's/[^:]\+: //' file

使用cut排除第一个以空格分隔的字段:

cut -d " " -f 2- file

Put the pattern which matches What, where, when , who inside a capturing group followed by a colon. 将与“什么,什么地方,什么时候,什么人”匹配的模式放在捕获组中,后跟冒号。 Then replace the matched chars with an empty string. 然后将匹配的字符替换为空字符串。 Add i flag at the last if you want do a case-insensitive match. 如果要进行不区分大小写的匹配,请在最后添加i标志。

$ sed 's/^Wh\(ere\|en\|at\|o\):[[:blank:]]*//' file
Tester1+Password
Authentication Success
Tues March 20, 2015 08:15:02 UTD
198.192.1.2

For general case, you could use 对于一般情况,您可以使用

sed 's/^[^[:blank:]]\+[[:blank:]]\+//' file
sed 's/^[^ ]* *//' YourFile

假设只有这四个字是可能的,并且作为您的样本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM