back toc next

Space Balls: Example

Print the first line as last

bash$ sed -n -e '1h;1!p;${g;p;}'
h: hold space <- pattern space
g: pattern space <- hold space

Emulation of tac

bash$ sed -n -e 'G;h;$p'
G: pattern space <<- '\n' hold space

Problem:
The output shows a exceeding newline at the end: it is because "G" adds a newline followed by the content of the hold buffer to the pattern buffer, even in the first line (which is printed at the end).

tac improved

bash$ sed -n -e 'G;h;$s/.$//p'
bash$ sed -n -e '1!G;h;$p'