Find the answer to your Linux question:
Results 1 to 6 of 6
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 ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2012
    Posts
    7

    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. #2
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Hi,

    You can try this:

    Code:
    #!/bin/bash
    declare -a args
    args=($@)
    for ((i=${#args[*]};i>0;i--)); do
     foo=${args[$(( $i - 1))]}
     printf "$foo "
    done
    echo

  3. #3
    Just Joined!
    Join Date
    Aug 2012
    Posts
    7
    the thing is that i am getting the input from a file not from STDIN
    so there might be more than one line .Also, the delimiter between sentences is dot(.)

  4. #4
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by eawedat View Post
    the thing is that i am getting the input from a file not from STDIN
    so there might be more than one line .
    easily solved.

    Also, the delimiter between sentences is dot(.)
    well, that made things a little trickier (for me anyway). i tried to get it work by changing Bash's IFS, but gave up: sed to the rescue.

    so i just took my original code and put it in a function, then i iterate over a file (specified in the code as inputFile) that has all "." chars replaced by newline chars, then each resulting line is passed to the reverse_line function.

    Code:
    #!/bin/bash
    
    reverse_line() {
      declare -a args
      args=($@)
      for ((i=${#args[*]};i>0;i--)); do
       foo=${args[$(( $i - 1))]}
       printf "$foo "
       [ $i == 1 ] && echo
      done
    }
    
    inputFile='./reverse-sentences.txt'
    ! [ -f $inputFile ] && echo "$inputFile: No such file" && exit 1
    
    while read line; do
      reverse_line $line
    done < <(cat $inputFile|xargs echo|sed -e 's|\.|\n|g')
    here's my example file containing sentences, and the script (called "reverse.sh") running on it:
    Code:
    [root@localhost]# cat reverse-sentences.txt
    this is the first sentence.  this is
     the 2nd one. yes
     i am the third
    one.
    [root@localhost]# ./reverse.sh
    sentence first the is this 
    one 2nd the is this 
    one third the am i yes 
    [root@localhost]#
    Last edited by atreyu; 09-04-2012 at 01:26 AM. Reason: typo

  5. #5
    Just Joined!
    Join Date
    Aug 2012
    Posts
    7
    thank you very much Mr.atreyu, you're very helpful. credits to you.

    I will try to do it as a challenge with no sed and I will paste the code.

  6. #6
    Trusted Penguin
    Join Date
    May 2011
    Posts
    4,353
    Quote Originally Posted by eawedat View Post
    thank you very much Mr.atreyu, you're very helpful. credits to you.
    np, happy to help/code!

    I will try to do it as a challenge with no sed and I will paste the code.
    when i have problems like this (text manipulation), my first instinct is to reach for Perl, which would be able to do all this stuff w/o any additional system calls. but if sed/awk/grep/bash can do it instead...well, great! i'd be interested in seeing your sed alternative.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •