如何用sed反转所有单词?

How to reverse all words in line with sed?

例如,我们有:

This is the song that doesn`t end

什么 sed 命令会把它变成这样?

end doesn`t that song the is This

我只找到了如何反转文件中的行 (a.k.a。tac):

sed -n '1!G;h;$p'

能否请您尝试关注,如果对您有帮助,请告诉我。

awk '{for(i=NF;i>0;i--){printf("%s%s",$i,(i>1?OFS:ORS))}}'  Input_file

这可能对你有用 (GNU sed):

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

附加一个换行符作为分隔符。将当前行分成三部分,并在第一个单词前面加上后面的 space 和换行符之后的行的其余部分。迭代直到模式匹配失败,然后移除引入的换行符。