Reversing and trim a String through SHELL script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Reversing and trim a String through SHELL script
# 1  
Old 09-26-2005
Reversing and trim a String through SHELL script

All

I want to reverse and trim a string using UNIX shell scripts.Iam using Bourne shells. Can you help me? Thanx in advance

Regards
Deepak
# 2  
Old 09-27-2005
Use the search facility.

Here is a result - transposing letters.

It talks about the command rev. It should do the reversing for you.

If rev is not available on your machine try this script below.

Code:
[~/temp]$ cat deepak.sh 
#! /bin/sh

# reverse a string

[[ -z "$@" ]] && STR=" a b c d e f g h i j k l m n o p q r s t u v w x y z " || STR="$@"

len=${#STR}

REV=""
for (( i=$len ; i>0 ; i-- ))
do
        REV=$REV""${STR:$i-1:$i}
        STR=${STR%${STR:$i-1:$i}}
done

echo "Reversed string"
echo $REV

Code:
[~/temp]$ ./deepak.sh
Reversed string
z y x w v u t s r q p o n m l k j i h g f e d c b a

As for trimming, you can trim the leading and trailing white spaces.

Use this constuct within the script.

Code:
# remove leading white spaces
REV=${REV## }
# remove trailing white spaces
REV=${REV%% }

vino
# 3  
Old 09-28-2005
Korne shell scritping commands

All
I want to reverse a string in Korne shells scripting. And Tell me how to convert the below line into korne shells format

for (( i=$len ; i>0 ; i-- ))

Thanx in advance.

Regards
Deepak
# 4  
Old 09-28-2005
Looks like you are duplicating this post - Reversing and trim a String through SHELL script .

First question. You have a script that works fine. Why change it for some other shell ? Unless its a homework question. In which case, the rules say

(6) Do not post classroom or homework problems.


Even if you get beyond the if else statement, the construct ${STR:$i-1:$i} is available only for sh. ksh and csh doesnt have this construct documented.

vino
# 5  
Old 09-28-2005
DeepakXavier, please do read those rules. You should have posted a followup to your original thread, not start a new thread.

Vino, Linux does not have a Bourne shell. It has bash which is a superset of of the old Bourne shell. Bash is linked to sh for people who use the old Bourne shell. Your script is using bash-only stuff.

For ksh, try...
Code:
#! /usr/bin/ksh

str="abcd"
rev=""
typeset -R1 last

while ((${#str})) ; do
        last=$str
        rev=${rev}${last}
        if ((${#str} > 1)) ; then
                typeset -L$((${#str}-1)) rest=$str
                str=$rest
        else
                str=
        fi
done
echo rev = $rev
exit 0

# 6  
Old 09-28-2005
to reverse a string - method I
*******************************


Code:
str="program"
echo $str | awk '{
for(i=length($0);i>=1;i--)
printf("%s",substr($0,i,1));
}'

to reverse a string - method II
********************************


Code:
str="program"
i=${#str}
final=""

while [ $i -gt 0 ]
do
  rev=`echo $str | awk '{printf substr($0, '$i', 1)}'`
  final=$final$rev
  i=$(($i - 1))
done

echo "Reversed String: $final"

to reverse a line
*************************************


Code:
str="this script is to reverse"
i=${#str}
word=""
fin=""
final=""

while [ $i -ge 0 ]
do
temp=`echo $str | awk '{printf substr($0, '$i', 1)}'`
  if [ \( "$temp" = " " \) -o $i -eq 0 ]
  then
     wordlen=${#word}
     while [ $wordlen -gt 0 ]
     do
        revtemp=`echo $word | awk '{printf substr($0, '$wordlen', 1)}'`
        fin=$fin$revtemp
        wordlen=$(($wordlen -1))
     done
     final=$final$fin" "
     fin=""
     word=""
     temp=""
  else
     word=$word$temp
  fi
i=$(($i - 1))
done

echo "Reversed line: $final"

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Trim String

Hi, I wish to grep everything before the last "/bin" in the following string /opt/app/bin/app1/jdk150_07/bin/IA64N/java Desired output: "/opt/app/bin/app1/jdk150_07" Kindly help ... (2 Replies)
Discussion started by: mohtashims
2 Replies

2. Shell Programming and Scripting

TRIM a string in shell script

HI, I have a string "NZ-deploy-mode-1.15-Linux.x86_64.rpm" I want to get the string before -1 ie "NZ-deploy-mode" Input is "NZ-deploy-mode-1.15-Linux.x86_64.rpm" expected output is "NZ-deploy-mode" How can I do that in shell script? Thanks in advance. (6 Replies)
Discussion started by: Ananthdoss
6 Replies

3. Shell Programming and Scripting

Can i use trim in shell script ?

Hi, I am stuck at comparing a value from database with a space involved. Condition if (cust_code != "RAMK" && cust_code != "LML") { xxxxxxx xxxxxxx xxxxxxx } the cust_code is from a table and generally has 4 chars...so in the case of LML it comes with default space at the end. ... (1 Reply)
Discussion started by: ramkiran77
1 Replies

4. Programming

Some error with number of keyboard inputs occured with this code for reversing a string..

i used a two-way linked list "node" for the code:: #include<stdio.h> #include<malloc.h> void insert(); void reverse(); struct node { char c; struct node *next; struct node *back; }*start=NULL; int main() { int n,i; (4 Replies)
Discussion started by: mscoder
4 Replies

5. UNIX for Dummies Questions & Answers

Newbie Alert:Reversing part of a string?

Hello all, Wondering if anyone can help me out. Trying to cut the names out of the /etc/passwd file so that they can be displayed first then last name. I cut them out and put them into variables but cant get them to display side by side after cutting. Anyone able to help a newbie? (1 Reply)
Discussion started by: losingit
1 Replies

6. Shell Programming and Scripting

How to trim a string in unix shell script

I am reading a string like $/folder1/folder2/filename from a file And I am storing it in a variable say $path. I want to store the path within single quotes in order to use the path within a command. if i set like path="'"$path"'" echo $path It is printing like ' $/folder1/folder2/filename'... (6 Replies)
Discussion started by: Shri123
6 Replies

7. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

8. Shell Programming and Scripting

Need help in shell script to trim numeric values

Hello I am currently working on a shell script which actually needs to pull some file from a server , The filenames will have the extension with date/time stamp , i need to trim that and name it to proper format Example : xyz_20091108_22142365.gzip i need to remove the... (5 Replies)
Discussion started by: ranga27
5 Replies

9. Shell Programming and Scripting

reading and reversing a string

Hi Everyone....I am new to Unix and BASH programming...I just want to read a string and reverse it and display.....can anyone help me out???? (8 Replies)
Discussion started by: nikhilneela
8 Replies

10. Shell Programming and Scripting

output string in reversing order

If I have string { I_love_shell_scripts} anyone knows how to have output {stpircs_llehs_evol_I} by using shell and perl ?I know in perl, there is reverse() funcation, but can it be done by not using reverse()? (3 Replies)
Discussion started by: ccp
3 Replies
Login or Register to Ask a Question