MOHAMED MOHAMED - 1 year ago 122
Bash Question

How to reverse a list of words in a shell string?

I have a list of words in a string:

str="SaaaaE SeeeeE SbbbbE SffffE SccccE"

I want to reverse it in order to get

"SccccE SffffE SbbbbE SeeeeE SaaaaE"

How I can do that with

Answer Source

You can use awk as follows:

echo $str | awk '{ for (i=NF; i>1; i--) printf("%s ",$i); print $1; }'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download