How to Reverse a sentence in linux ? (bash/cshell)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Reverse a sentence in linux ? (bash/cshell)
# 1  
Old 09-02-2012
How to Reverse a sentence in linux ? (bash/cshell)

hey all,

If I have the given sentence.

Code:
I like bobo.

How could I reverse it to be :

Code:
.bobo like I

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).

thank you.
# 2  
Old 09-02-2012
Code:
 perl -alne 'for($i=$#F;$i>=0;$i--){printf("%s ",$F[$i]);}' input_file

# 3  
Old 09-02-2012
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.

Code:
(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)
[~]$

# 4  
Old 09-02-2012
Not able to find how to do with FS....Smilie.....Smilie
With default FS as space it works perfect....Smilie

!)
Code:
awk '{ for (i=NF;i>=1;i--) { if(s){s=s" "$i}else{s=$i }}{print s;s=""}}' file

2)
Code:
$ echo "12345   ddjjdj  djdd dndj"|rev
jdnd ddjd  jdjjdd   54321


Last edited by pamu; 10-15-2012 at 07:21 AM.. Reason: Added 2nd method..
# 5  
Old 09-02-2012
The period stays a problem, as DeCoTwc already mentioned:
Code:
 set -- $(cat test); for (( i=$#; i>0; i-- )); do eval echo -n \$$i"\ "; done; echo

I know, using "eval" may expose your system to a certain risk.
# 6  
Old 09-02-2012
Question

Quote:
Originally Posted by eawedat
hey all,

If I have the given sentence.

Code:
I like bobo.

How could I reverse it to be :

Code:
.bobo like I

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).

thank you.
You don't define what you mean by "word", but since you want.boboinstead ofbobo.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:
Code:
#!/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.)
Code:
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

Hope this helps...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

How to reverse compiled bash (obfucation)?

hi guys, 10 years a go I wrote an script in bash programming and I compiled (obfuscate) it, but after 10 years I need to change some lines and remove some lines, but i do not remember what I've done. does anyone has any idea about decompile (deobfuscation) it? here is some line of my code (u... (3 Replies)
Discussion started by: mhsh0001
3 Replies

2. Red Hat

Bash: menu-complete and reverse

Hi, In the archives I found this: And this works fine. $if mode=vi "\C-0-": digit-argument TAB: menu-complete "\e But what I want is to reverse this. So I want that tab does reverse menu completion and shift tab does normal menu completion. Can anyone help me with this? Thanks (0 Replies)
Discussion started by: ozkanb
0 Replies

3. IP Networking

HOWTO: Linux multihomed dns client - reverse lookup

The following thread is closed: 133552-howto-linux-multihomed-dns-client (Sorry I am not allowed to post URLs) Therefore I write this append in an own thread. The HOWTO in the referenced thread helped me a lot and I only want to append how to make reverse lookup working for a local zone: ... (0 Replies)
Discussion started by: scheruga
0 Replies

4. Shell Programming and Scripting

Searching for bash to cshell convertor

Hi ,I have seen this comverters before ,but I was searching today and I can not find them .Is anybody used them before .Any recomendations Thanks (5 Replies)
Discussion started by: lio123
5 Replies

5. Shell Programming and Scripting

[grep] how to grep a sentence which has quotation marks "sentence"

I would like to check with grep in this configuration file: { "alt-speed-down": 200, "alt-speed-enabled": true, "alt-speed-time-begin": 1140, "alt-speed-time-day": 127, "...something..." : true, ... } "alt-speed-enabled" (the third line of the file) is setted to... (2 Replies)
Discussion started by: ciro314
2 Replies

6. UNIX for Dummies Questions & Answers

Regarding Decimals in Cshell

Hello... I am new to unix and I am wondering if in a C-shell script , Are we supposed to use only whole numbers........ for example..if a program needs to calculate the average of some numbers........ @ avg = (($1 +$2 + $3)/3)) is returning a whole number.........How can a decimal be achieved... (1 Reply)
Discussion started by: ravindra22
1 Replies

7. UNIX for Dummies Questions & Answers

Script to ask for a sentence and then count number of spaces in the sentence

Hi People, I need some Help to write a unix script that asks for a sentence to be typed out then with the sentence. Counts the number of spaces within the sentence and then echo's out "The Number Of Spaces In The Sentence is 4" as a example Thanks Danielle (12 Replies)
Discussion started by: charlie101208
12 Replies

8. UNIX for Dummies Questions & Answers

grep in Cshell

Hi Everyone, I'm facing a problem using grep in Cshell. This is what i'm trying to do: grep "abc" somefile VAR="$?" echo $VAR somefile contains: abc def ghi Now, should'nt my output be 0 (Zero) I'm getting 1 (One) Can you please help me out. Thanks in advance :) G1 (2 Replies)
Discussion started by: jeevan_fimare
2 Replies

9. UNIX for Advanced & Expert Users

linux reverse page order+duplex is not working

hi all, i have a postscript file with duplex print commands. When i print it with lpr command it prints in duplex. lpr -pprintername filename.ps but when i try to print the pages in reverse order with -outputorder=reverse it is not printing in reverse order (but pages are prited... (0 Replies)
Discussion started by: uttamhoode
0 Replies

10. Shell Programming and Scripting

Bash script pass sentence in block

Hello, I want to know is it possible to pass a block of sentence using bash. For example, I have a script called Test.sh that takes in $1 and $2. and I'm calling Test.sh in a.sh so in a.sh Test.sh '' 'This is a sentence' Because block are separated by space so when I do that, I get... (6 Replies)
Discussion started by: katrvu
6 Replies
Login or Register to Ask a Question