Issue
The following text has 2 address ranges that will be matched
using sed -n '/#/,/\$/p'
:
foo
#bar
foo
$
foobar
#foo
$bar
foobar
How can I delete a specific match only (1st, 2nd...)? Below should be the output for deleting 2nd match only:
foo
#bar
foo
$
foobar
foobar
Solution
This might work for you (GNU sed):
sed -E '/#/{x;s/^/x/;x};/#/,/\$/{x;/^x{2}$/{x;d};x}' file
Set up a counter in the hold space and use it to determine which range to delete.
This has the added advantage of being able to delete a range of ranges e.g.
sed -E '/#/{x;s/^/x/;x};/#/,/\$/{x;/^x{2,3}$/{x;d};x}' file
This will delete the second and third ranges.
Answered By - potong Answer Checked By - Gilberto Lyons (WPSolving Admin)