-1

For example, we have:

This is the song that doesn`t end

What sed command will turn it into this?

end doesn`t that song the is This

I've found only how to reverse lines in a file (a.k.a. tac):

sed -n '1!G;h;$p'
Benjamin W.
  • 46,058
  • 19
  • 106
  • 116
B1ZON
  • 13
  • 2
  • 1
    Why would you think sed would be a reasonable choice for this? sed is for `s/old/new/`, that is all. – Ed Morton Jul 20 '18 at 13:44
  • 1
    You could adapt [this sed script](https://www.gnu.org/software/sed/manual/sed.html#Reverse-chars-of-lines) to swap words instead of characters, but it's almost certainly going to be a brittle, unreadable mess. – Benjamin W. Jul 20 '18 at 14:52

2 Answers2

0

Could you please try following and let me know if this helps you.

awk '{for(i=NF;i>0;i--){printf("%s%s",$i,(i>1?OFS:ORS))}}'  Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
0

This might work for you (GNU sed):

sed -r 'G;:a;s/^(\S+)(\s*)(.*\n)/\3\2\1/;ta;s/\n//' file

Append a newline as a delimiter. Split the current line into three and prepend the first word, the following space and the remainder of the line following the newline in that order. Iterate until the pattern matching fails and then remove the introduced newline.

potong
  • 55,640
  • 6
  • 51
  • 83