I currently use this bash for loop below to reverse a set of tokens, example "abc def ghi" to "ghi def abc" but in looking at various sed one liner postings I notice two methods to reverse lines of text from a file (emulating tac) and reversing letters in a string (emulating rev) so I've spent some time on how to reverse string tokens using sed, but I'm getting nowhere fast. Any suggestions welcome on how to use sed to reverse a series of string tokens as per this for loop example.
TOKENS="abc def ghi"; for i in $TOKENS; do STR="$i $STR"; done; echo $STR
ghi def abc
And these are the 2 sed one liners...
# reverse order of lines (emulates "tac")
sed '1!G;h;$!d' # method 1
sed -n '1!G;h;$p' # method 2
# reverse each character on the line (emulates "rev")
sed '/\n/!G;s/\(.\)\(.*\n\)/&\2\1/;//D;s/.//'