Reverse print order

Score
0
Vote Up
mibwalker April 3, 2003 at 14:00:47 Pacific
Specs: AIX 4.1, rs6000

I would like to know if there is a way or a script that would allow me to read a text string and then output it in reverse order.
Example:
for i in `cat $1`
do
print $i
done
If i in is "Test" I would like the output to be "tseT"


Reply ↓  Report •


#1
Vote Down
Score
0
Vote Up
April 3, 2003 at 14:48:16 Pacific

There is probably a way Perl or awk, but how about converting the string to an array of letters, and looping backwards through it:

#!/bin/ksh

string="$*"
integer i=${#string}
set -A arrayvar $(print $* | sed 's/./& /g')

while (( i >= 0 ))
do
print -n ${arrayvar[$i]}
(( i -= 1 ))
done

print


Reply ↓  Report •

#2
Vote Down
Score
2
Vote Up
April 4, 2003 at 06:18:07 Pacific

This works on HP-UX:

echo $1 | rev


Reply ↓  Report •

#3
Vote Down
Score
0
Vote Up
James Booth April 7, 2003 at 07:28:45 Pacific

That was a great answer, I knew there had to be a real easy way to do it. Thanks

Reply ↓  Report •

#4
Vote Down
Score
2
Vote Up
April 7, 2003 at 15:01:47 Pacific

That's weird. It would appear that I am answering myself (Oh, am I?). The previous post appears to have come from me, but it was not my reply. The name is slightly misspelled, but click on the link and you see my e-mail address.

OK, who did that?


Reply ↓  Report •

Related Posts

#5
Vote Down
Score
2
Vote Up
April 7, 2003 at 15:59:51 Pacific

Well it wasn't me.

Reply ↓  Report •

Reply to Message Icon Start New Discussion
« Unix for PC ? shell script help! »

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.

Ask the Community!
Describe your Problem
Example: Hard Drive Not Detected on My PC