Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

What is the best way to turn out digits in number?

E.g.

$ echo 123 | hook
321
$ echo 12358 | hook
85321 
share|improve this question

6 Answers 6

up vote 8 down vote accepted
:~> echo 123456 | rev
654321

It should work. The usage is pretty simple. from man:

NAME
rev - reverse lines of a file or files

SYNOPSIS
rev [file ...]

DESCRIPTION
The rev utility copies the specified files to the standard output, reversing the order of characters in every line. If no files are specified, the standard input is read.

If not available you should also be able to use awk:

:~> echo 123456 | awk '{ for(i=length;i!=0;i--)x=x substr($0,i,1);}END{print x}'
654321

Source the unix school

If you eventually need to completly invert a file you could add tac to rev command line like this:

tac myfile | rev 
share|improve this answer
    
Which definition of character does it use? Byte, code-unit, code-point, or does it even a full combining character sequence? –  CodesInChaos Mar 24 at 18:10
    
What? I dont understand your question –  Kiwy Mar 24 at 18:12
    
I'm interested in how well rev understands text (and thus charsets and unicode), or if it simply operates on bytes. Consider an input which contains characters outside ASCII. For example with UTF-8, one code-point often maps to two bytes. If you reverse the bytes, that will produce a broken output. Same for legacy multi-byte charsets like they're common in Asia. Similarly if you have a letter followed by a combining diacritic, simply reversing the code-points means that it will combine with the wrong base letter. –  CodesInChaos Mar 24 at 18:20
2  
@CodesInChaos rev operates on bytes, but uses the current locale to interpret them. Compare LANG=utf8 rev file.txt vs LANG=C rev file.txt for some UTF-8 encoded file with non-ASCII characters. With utf8, rev will recognize multibyte UTF-8 sequences and preserve their relative order. –  chepner Mar 24 at 19:10
2  
@chepner, GNU rev reverses the order of characters in each line, not bytes, not combining sequences. It will turn Stéphane to enahpétS but Stéphane to enahṕetS (that is the accent will go to the p instead of the e) –  Stéphane Chazelas Mar 24 at 20:00

For those not limited to bash, with zsh:

forward=123
backward=${(j::)${(Oas::)forward}}

Those are variable expansion flags:

  • s::: split on the empty string, so one element per character
  • Oa: reverse the resulting array (O for reverse order, a for array subscript order).
  • j::: join the elements with the empty string.
share|improve this answer

Are you looking for rev?

echo 123|rev
321
share|improve this answer
#!/bin/bash
#set -xv

string="$1"
reverse_string=""
string_length="${#string}"

while (( "$string_length">=1 )) 
do
     string_length=$(( $string_length - 1 ))
     reverse_string="$reverse_string${string:$string_length:1}"
done

echo "$reverse_string"

make the file executable

$ chmod +x reverse_string
$ ./reverse_string 123456789    
98765432    
share|improve this answer
    
Or somewhat more compactly: for ((i=${#1}-1; i>=0; i--)); do echo -n "${1:i:1}"; done; echo –  DigitalTrauma Mar 24 at 19:02

rev is the obvious and probably the best answer. But rev is not always installed by default. If you want to do this purely with coreutils you can use this longer command:

echo 123 | fold -w1 | tac | paste -d '' -s -
  • fold -w1 puts each digit on a line
  • tac is a reverse cat, i.e. reverses the order of the lines
  • paste -d '' -s - serially pastes the digit from each line back onto one line
share|improve this answer
1  
Neither rev nor tac are standard Unix commands, but rev is a lot more portable than tac. tac is GNU only. Other unices usually have tail -r for tac. –  Stéphane Chazelas Mar 24 at 19:55
s=""
for i in $*
do
    s="$i $s";
done
for j in $s
do
    echo $j
done
share|improve this answer
2  
How is that answering the question? –  Stéphane Chazelas May 23 at 12:37
    
It's clearly attempting to, it just gets it kind of wrong. He wrote a script that will output its arguments in reverse order, instead of a script that will reverse its stdin –  Michael Mrozek May 23 at 15:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.