FYI, you can read how to do it with
sed here:
http://sed.sourceforge.net/sedfaq4.html#s4.26
As you can see,
sed isn't really well-designed for multiple-line editing, particularly when it involves moving "back" in the file. The usual workflow for it is one-way.
Incidentally, this can also be done with
ed fairly easily.
Code:
ed -s file.txt <<HEREDOC
#start global command list -- find lines starting with "return", move back one, and
#add a space to the end. final backslash means the "g" setting continues to the next command.
g/^return/-1 s/$/ /\'
#join the current line (the one just modified above) with the one after it (end of global command list).
.,+1j
#print the altered buffer to stdout.
%p
#write the buffer back to the original file.
w
#write the buffer to a new file.
w newfile.txt
HEREDOC
I used the heredoc format so I could add comment lines to the script, but any technique that feeds the commands to stdin separated by linefeeds will work. e.g., more compactly:
Code:
printf '%s\n' 'g/^return/-1 s/$/ /\' '.,+1j' '%p' | ed -s file.txt
One limitation, however, that it will error out if "return" ever happens to be the first line of the file.
How to use ed:
http://wiki.bash-hackers.org/howto/edit-ed
http://snap.nlc.dcccd.edu/learn/nlc/ed.html
(also read the info page)