简体   繁体   中英

Delete all the matching lines except the first occurence of the Pattern

I have the below lines

logs/backups
logs/backups/config_bk

logs/backups/cofig_bkp other/lines
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:

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:

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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