Issue
I have read through dozens of sed questions, none of them are related to what I'm trying to do. I am using the sed c
command to match multiple lines within a file, but I need the contents between the two sections.
a
begin
c
d
end
f
...
I need to capture the 2 lines c
and d
in this example and remove the new lines between, and use them in the output, but nothing I have tried or can find allows me to capture from the match expression.
With some pseudocode containing \1
for the capture group output:
cat file | sed -e '/begin/,/end/c\begin\n\1\nend'
This should produce
a
begin
c
d
end
f
...
And I need it to not execute if a match is not found.
I don't know if sed is the right tool for the job, but it may be just 1 step outside of its use case. I'm open to alternatives.
Solution
One approach would be:
sed '/begin/,/end/{ /^$/d; }' file
Answered By - M. Nejat Aydin Answer Checked By - Candace Johnson (WPSolving Volunteer)