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
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
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.
[~/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
[~/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.
# remove leading white spaces
REV=${REV## }
# remove trailing white spaces
REV=${REV%% }
vino
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
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
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...
#! /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
to reverse a string - method I
str="program"
echo $str | awk '{
for(i=length($0);i>=1;i--)
printf("%s",substr($0,i,1));
}'
to reverse a string - method II
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
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"