When you use grouped commands in a sed script, is there a way to reference the first and last line of the range you're in?
I want to be able to print the first and last line of a range as well as selected lines between those.
#n
/StartLinePattern/,/EndLinePattern/{
/PatternOfSubLineToPrint/p;
}
I know that I can solve this by including another grouped command that matches the first and last lines of the range (again); but it would be cleaner, faster, and more reusable to do something akin to the standard for non-grouped commands.
1p;$p
I tried including the above in the group, but it doesn't work. It appears that 1
and $
are absolute, not relative to the range you're in.
Background
I made a sed script that filters elements out of an XML file. To do this, I use ranges with grouped commands to print certain sub elements within that range. So the script works by printing everything that you want to keep.
#n
/<Parent\>/,<\/Parent>/{
/<Child1\>/,/<\/Child1>/p;
/<Child2\>/,/<\/Child2>/p;
/<SingleLineChild\>/p;
}
sed
is not a XML parser – Gilles Quénot Jan 18 '23 at 17:54