简体   繁体   English

删除除第一次出现的 Pattern 之外的所有匹配行

[英]Delete all the matching lines except the first occurence of the Pattern

I have the below lines我有以下几行

logs/backups日志/备份
logs/backups/config_bk日志/备份/config_bk

logs/backups/cofig_bkp other/lines日志/备份/cofig_bkp 其他/行
and/some/more/lines和/一些/更多/行

I have to search for a string "logs/backups" and need to delete all the lines which is having logs/backups except the first occurence... output which i need:我必须搜索字符串“日志/备份”,并且需要删除所有有日志/备份的行,除了第一次出现... output 我需要:

logs/backups日志/备份

other/lines其他/线路

and/some/more/lines和/一些/更多/行

If the task is to preserve logs/backups , but not logs/backups/* , the solution is quite simple:如果任务是保留logs/backups ,而不是logs/backups/* ,则解决方案非常简单:

egrep  -v '^logs/backups.' <filename>

This will filter out (-v) any line that starts (represented by a caret) with 'logs/backups' and has at least one more chracter (represented by a dot).这将过滤掉 (-v) 任何以“日志/备份”开头(由插入符号表示)并且至少还有一个字符(由点表示)的行。

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

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