![]() |
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.
|
|
google unix.com
|
|||||||
| Forums | Register | Forum Rules | Links | Albums | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here. |
More UNIX and Linux Forum Topics You Might Find Helpful
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| How to trim the white space around a string in C program | hxm1303 | High Level Programming | 18 | 10-17-2008 10:43 AM |
| TRIM spaces in shell | anumkoshy | UNIX for Advanced & Expert Users | 3 | 08-31-2007 04:13 AM |
| trim leading zero in certain column in a string | dngo | UNIX for Dummies Questions & Answers | 2 | 04-01-2007 02:30 PM |
| output string in reversing order | ccp | Shell Programming and Scripting | 3 | 11-19-2005 11:05 AM |
| Trim string length using awk -F? | TheCrunge | Shell Programming and Scripting | 3 | 08-01-2005 10:41 AM |
![]() |
|
|
LinkBack | Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
|||||
|
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 Use this constuct within the script. Code:
# remove leading white spaces
REV=${REV## }
# remove trailing white spaces
REV=${REV%% }
|
|
||||
|
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));
}'
******************************** 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"
************************************* 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"
|
|
|||||
|
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 |
![]() |
| Bookmarks |
| Tags |
| awk, awk trim, linux, trim, trim awk |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|