in reply to Five Ways to Reverse a String of Words (C#, Perl 5, Perl 6, Ruby, Haskell)
This thread has no sed solution yet?
The following program reads a single line, and gives its words in reverse.
sed 's/^/@/;:;s/\(.*@\)[ \t]*\([^ \t][^ \t]*\)/\2 \1/;t;s/ *@.*//;q'
(This works in GNU sed. If you use another sed, you may need to change the semicolons to new lines, and the \t escapes to real tab characters.)
This really breaks if the input contains an at-sign. If you need such inputs, change the at-sign to some other character that doesn't occurr in the input, or use the following more complicated snippet.
sed 's/@/@a/g;s/^/@s/;:;s/\(.*@s\)[ \t]*\([^ \t][^ \t]*\)/\2 \1/;t;s/