简体   繁体   中英

Using sed, how can I delete all instances of a string within a range of lines?

I know that

sed '/match/ d' file

deletes all lines with matches, and

sed '1,3 s/match//g' file

deletes all matches in the first 3 lines.

But how do I delete all lines with matches in first 3 lines?

If possible, give a solution with only one sed call (no piping).

You could combine the two:

sed '1,3{/match/d;}' file

This would delete lines containing match in the specified address range, ie in lines 1-3 in the example above.

如果你想测试awk ,那将是:

awk '/match/ && NR<=3 {next} 1' file
awk -v string="foo" 'NR<=3 && index($0,string) {next} 1' file

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