The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



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

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 09-26-2005
DeepakXavier DeepakXavier is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 19
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 (permalink)  
Old 09-27-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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 (permalink)  
Old 09-28-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,111
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
  #4 (permalink)  
Old 09-28-2005
matrixmadhan matrixmadhan is online now Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,951
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"
  #5 (permalink)  
Old 09-28-2005
DeepakXavier DeepakXavier is offline
Registered User
  
 

Join Date: Sep 2005
Posts: 19
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
  #6 (permalink)  
Old 09-28-2005
vino's Avatar
vino vino is offline Forum Staff  
Supporter (in vino veritas)
  
 

Join Date: Feb 2005
Location: Bangalore, India
Posts: 2,796
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
Closed Thread

Bookmarks

Tags
awk, awk trim, linux, trim, trim awk

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 03:13 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0