bindbn
bindbn

Reputation:

reverse the order of characters in a string

In string "12345", out string "54321". Preferably without third party tools and regex.

Upvotes: 104

Views: 129951

Answers (14)

kenorb
kenorb

Reputation: 166755

Try Perl:

echo 12345 | perl -nle 'print scalar reverse $_'

Source: Perl one-liners

Upvotes: 0

MadHatter
MadHatter

Reputation: 2298

I know you said "without third-party tools", but sometimes a tool is just too obviously the right one, plus it's installed on most Linux systems by default:

[madhatta@risby tmp]$ echo 12345 | rev
54321

See rev's man page for more.

Upvotes: 219

anubhava
anubhava

Reputation: 785711

Here is another simpler awk solution:

awk 'BEGIN{FS=""} {for (i=NF; i>0; i--) s=s $i; print s}' <<< '123456'

654321

Upvotes: 2

Nikhil
Nikhil

Reputation: 1

read word

reve=`echo "$word" | awk '{for(i=length($0); i>0;i--) printf (substr($0,i,1));}'`
echo  "$reve"

Upvotes: -1

Michael Back
Michael Back

Reputation: 1871

For those without rev (recommended), there is the following simple awk solution that splits fields on the null string (every character is a separate field) and prints in reverse:

awk -F '' '{ for(i=NF; i; i--) printf("%c", $i); print "" }'

The above awk code is POSIX compliant. As a compliant awk implementation is guaranteed to be on every POSIX compliant OS, the solution should thus not be thought of as "3rd-party." This code will likely be more concise and understandable than a pure POSIX sh (or bash) solution.

(; I do not know if you consider the null string to -F a regex... ;)

Upvotes: 7

ghoti
ghoti

Reputation: 46876

Nobody appears to have posted a sed solution, so here's one that works in non-GNU sed (so I wouldn't consider it "3rd party"). It does capture single characters using the regex ., but that's the only regex.

In two stages:

$ echo 123456 | sed $'s/./&\\\n/g' | sed -ne $'x;H;${x;s/\\n//g;p;}'
654321

This uses bash format substitution to include newlines in the scripts (since the question is tagged ). It works by first separating the input string into one line per character, and then by inserting each character into the beginning of the hold buffer.

  • x swaps the hold space and the pattern space, and
  • H H appends the (current) pattern space to the hold space.

So for every character, we place that character into the hold space, then append the old hold space to it, thus reversing the input. The final command removes the newlines in order to reconstruct the original string.

This should work for any single string, but it will concatenate multi-line input into a single output string.

Upvotes: 3

David Heffernan
David Heffernan

Reputation: 81

Some simple methods of reversing a string

echo '!!!esreveR si sihT' | grep -o . | tac | tr -d '\n' ; echo

echo '!!!esreveR si sihT' | fold -w 1 | tac | tr -d '\n' ; echo

Convert to hex values then reverse

echo '!!!esreveR si sihT' | xxd -p | grep -o .. | tac | xxd -r -p ; echo

echo '!!!esreveR si sihT' | xxd -p | fold -w 2 | tac | xxd -r -p ; echo

Upvotes: 8

user8017719
user8017719

Reputation:

If var=12345:

for((i=0;i<${#var};i++)); do rev="$rev${var:~i:1}"; done

c=$var; while [ "$c" ]; do rev=$rev${c#"${c%?}"}; c=${c%?}; done

echo "var: $var, rev: $rev"

Run it:

$ rev
var: 12345, rev: 54321

Upvotes: 5

user2350426
user2350426

Reputation:

A bash solution improving over @osdyng answer (my edit was not accepted):

var="12345"     rev=""

for(( i=0 ; i<${#var} ; i++ )); do rev="${var:i:1}$rev"; done

echo "var: $var, rev: $rev"

Or an even simpler (bash) loop:

var=$1   len="${#var}"   i=0   rev=""

while (( i<len )); do rev="${var:i++:1}$rev"; done

echo "var: $var, rev: $rev"

A POSIX solution:

var="12345"     rev=""    i=1

while  [ "$i" -le "${#var}" ]
do     rev="$(echo "$var" | awk -v i="$i" '{print(substr($0,i,1))}')$rev"
       : $(( i+=1 ))
done

echo "var: $var, rev: $rev"

Note: This works on multi byte strings. Cut solutions will work only in ASCII (1 byte) strings.

Upvotes: 9

Nono
Nono

Reputation: 31

This can of course be shortened, but it should be simple to understand: the final print adds the newline.

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

Upvotes: 3

Nick
Nick

Reputation: 509

Presume that a variable 'var' has the value '123'

var="123"

Reverse the string and store in a new variable 'rav':

rav=$(echo $var | rev)

You'll see the 'rav' has the value of '321' using echo.

echo $rav

Upvotes: 16

Lri
Lri

Reputation: 27623

rev | tail -r (BSD) or rev | tac (GNU) also reverse lines:

$ rev <<< $'12\n34' | tail -r
43
21
$ rev <<< $'12\n34' | gtac
43
21

If LC_CTYPE is C, rev reverses the bytes of multibyte characters:

$ LC_CTYPE=C rev <<< あの
��め�
$ export LC_ALL=C; LC_ALL=en_US.UTF-8 rev <<< あの
のあ

Upvotes: 13

Dennis Williamson
Dennis Williamson

Reputation: 360445

This reverses the string "in place":

a=12345
len=${#a}
for ((i=1;i<len;i++)); do a=$a${a: -i*2:1}; done; a=${a:len-1}
echo $a

or the third line could be:

for ((i=0;i<len;i++)); do a=${a:i*2:1}$a; done; a=${a:0:len}

or

for ((i=1;i<len;i++)); do a=${a:0:len-i-1}${a: -i:i+1}${a:len-i-1:1}; done

Upvotes: 6

osdyng
osdyng

Reputation:

Simple:

var="12345"
copy=${var}

len=${#copy}
for((i=$len-1;i>=0;i--)); do rev="$rev${copy:$i:1}"; done

echo "var: $var, rev: $rev"

Output:

$ bash rev
var: 12345, rev: 54321

Upvotes: 38

Related Questions