Portable way to remove the first line from the pattern space (when multiple lines are present)

sed

What's the portable way to delete the first line1 from the pattern space ?
With gnu sed I can do

s/[^\n]*\n//

but as far as I know this (using \n in a bracket [] expression) is not portable.


Practical example: here, sed prints the last section of the file including the delimiter via portable code. I'd like to remove the first line from the pattern space so as to exclude the delimiter and do that in a portable manner. With gnu sed it's simple:

sed 'H;/===/h;$!d;//d;x;s/[^\n]*\n//' infile

1: Obviously this should be done without restarting the cycle of commands…

Best Answer

One way to portably do this sort of a thing is as follows:

sed -e '
   # ... assuming prev sed cmds made pattern space carry newline(s)
   y/\n_/_\n/     ;# exchange newlines with an underscore
   s/^[^_]*_//    ;# remove up till the first underscore, ummm newline
   y/\n_/_\n/     ;# revert the transformation
' 
Related Question