I am NOT talking about reversing words or characters by using rev. I am talking about reversing the whole sentence(placement of words in the given sentence).
Neither one of these is really perfect, as it leaves the period placement wonky. You could fix it, but this seems like kind of a silly exercise to put that much effort into.
(05:13:08\[root@DeCoBoxOmega)
[~]$ cat sentence
This seems like a dumb excercise.
(05:14:30\[root@DeCoBoxOmega)
[~]$ sent=($(cat sentence));count="${#x[@]}";until [[ ${count} -eq 0 ]];do printf "%s " ${sent[$count]/.//};let count=count-1;done;printf "." #In pure bash, leaves spacing issues
excercise/ dumb a like seems .
(05:14:59\[root@DeCoBoxOmega)
[~]$ tr " " "\n" < sentence |tac| tr "\n" " " #Using tr, puts period in wrong place
excercise. dumb a like seems This
(05:15:22\[root@DeCoBoxOmega)
[~]$
You don't define what you mean by "word", but since you want .bobo instead of bobo. it is clear that you aren't using the same definition used by documentation of the shell.
Most of the responses on this thread so far use the definition of word normally used by the shell and assume that the separator to be output between words is always a space. Some of them even terminate the output line(s) with a space instead of a <newline> character.
You don't say what is supposed to happen to the separators between words (except for one example of a period). You also don't say whether your input is limited to a single line (as in your example) or whether each line is to be reversed in multi-line input. The following awk script will process all lines in a text file (even if the input is an empty file). It assumes that the definition of word is a contiguous string of alphanumeric characters. And it treats any contiguous string of non-alphanumeric characters as a word separator and preserves them in the output in the given order.
This script won't create any security holes and will work with any mostly POSIX-conforming shell (at least bash, sh, and ksh); it won't work with csh or tcsh:
#!/bin/ksh
awk ' { remains = $0
out = ""
printf("Input is: \"%s\"\n", $0)
while(remains != "") {
# Move the word (stirng of contiguous alphanumeric character) or
# separator (string of contiguous non-alphanumeric characters)
# at the in of the remaining input string to the end of the
# output string.
match(remains, /([[:alnum:]]+|[^[:alnum:]]+)$/)
out=out substr(remains, RSTART, RLENGTH)
remains=substr(remains, 1, length(remains) - RLENGTH)
}
print out
}'
The output produced with several test cases is shown below. (You can remove the printf that displays the input lines if you want to, but it helps here so you can see the transformtions it performs.)
Input is: "I like bob."
.bob like I
Input is: "This doesn't have all non-alpahnumerics as whitespace characters abc-def."
.def-abc characters whitespace as alpahnumerics-non all have t'doesn This
Input is: "This has two spaces between words."
.words between spaces two has This
Input is: "This has a <tab> between words."
.words between> tab <a has This
Input is: "This has a <space> and a <tab> between words."
.words between> tab <a and> space <a has This
Input is: "<space><tab> at end "
end at> tab><space<
Input is: "alpha at end"
end at alpha
Input is: "numeric at end 123"
123 end at numeric