sed或awk删除匹配正则表达式的行以及以下行

时间:2018-08-29 18:04:18

标签: bash awk sed

我正在过滤日志文件,并根据类别类型将其分为几部分。我将其用于1个类别:

sed -e '/Associating reconciliation identity to/{N;N;d}' source.log  > destination.txt

当我搜索其他字符串并尝试删除THAT行以及随后的7行时,出现了我遇到的问题。我不是100%sed是最好的工具,但我还没有得到awk的帮助。根据以上示例,我认为这应该可行:

sed -e '/Found multiple matches (instances) for class/{N;N;N;N;N;N;N;N;d}' source.log  > destination.txt

有更好的方法吗?


请求添加:

Source.log

<ERROR   > <TID: 140100592498432> /* Wed Aug 29 2018 05:18:20.7938 */ Found multiple matches (instances) for class <Software.Class> in look-up dataset <Category> 

<ERROR   > <TID: 140100592498432> /* Wed Aug 29 2018 05:18:20.7939 */ for qualification <'Name' != $\NULL$ AND 'Name' = $Name$> of group <Job-ID> with values 

<ERROR   > <TID: 140100592498432> /* Wed Aug 29 2018 05:18:20.7939 */ < Name = software.example >

<ERROR   > <TID: 140100592498432> /* Wed Aug 29 2018 05:18:20.7939 */ < RelLeadClassId = NULL >

<ERROR   > <TID: 140100592498432> /* Wed Aug 29 2018 05:18:20.7939 */ < RelLeadInstanceId = NULL >

<ERROR   > <TID: 140100592498432> /* Wed Aug 29 2018 05:18:20.7939 */ < RequestId = 00000000000|00000000000|000000000000 >

<ERROR   > <TID: 140100592498432> /* Wed Aug 29 2018 05:18:20.7939 */ < Secondary_UID = NULL >

<ERROR   > <TID: 140100592498432> /* Wed Aug 29 2018 05:18:20.7939 */ < TokenId = HashToken87hnUIKW22er4xbs78== >






<ERROR   > <TID: 140100593551104> /* Wed Aug 29 2018 05:18:20.7946 */ Found multiple matches (instances) for class <Software.Class> in look-up dataset <Category> 

<ERROR   > <TID: 140100593551104> /* Wed Aug 29 2018 05:18:20.7946 */ for qualification <'Name' != $\NULL$ AND 'Name' = $Name$> of group <Job-ID> with values 

<ERROR   > <TID: 140100593551104> /* Wed Aug 29 2018 05:18:20.7946 */ < Name = software.version >

<ERROR   > <TID: 140100593551104> /* Wed Aug 29 2018 05:18:20.7946 */ < RelLeadClassId = NULL >

<ERROR   > <TID: 140100593551104> /* Wed Aug 29 2018 05:18:20.7946 */ < RelLeadInstanceId = NULL >

<ERROR   > <TID: 140100593551104> /* Wed Aug 29 2018 05:18:20.7946 */ < RequestId = 000000000|000000000000|000000000000>

<ERROR   > <TID: 140100593551104> /* Wed Aug 29 2018 05:18:20.7946 */ < SERVICE_NOW_UID = NULL >

<ERROR   > <TID: 140100593551104> /* Wed Aug 29 2018 05:18:20.7946 */ < TokenId = HashTokenHHK72nsyUIOI92bYH== >

2 个答案:

答案 0 :(得分:2)

尝试一下:

sed -e '/<search string here>/,+7d' source.log  > destination.txt

答案 1 :(得分:1)

这将为您工作:

grep -A7 -P 'regexhere' file| xargs -I {} sed -i.bak 's/{}//g' 1txt 

说明:

grep -A7在正则表达式匹配后找到下7行-P是grep中的perl正则表达式

然后我们使用xargs从grep捕获输出作为行,并在sed命令中使用整行作为替换。

-i.bak选项仅创建我们要更改的文件的备份

附言:上面的代码将删除行,您随时可以选择替换并进行修改。