7

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

E.g.

$ echo 123 | hook
321
$ echo 12358 | hook
85321 
1

8 Answers 8

11
:~> 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 
7
  • Which definition of character does it use? Byte, code-unit, code-point, or does it even a full combining character sequence? Mar 24, 2014 at 18:10
  • What? I dont understand your question
    – Kiwy
    Mar 24, 2014 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. Mar 24, 2014 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, 2014 at 19:10
  • 3
    @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) Mar 24, 2014 at 20:00
6

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.
0
2

Are you looking for rev?

echo 123|rev
321
2
#!/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    
1
  • Or somewhat more compactly: for ((i=${#1}-1; i>=0; i--)); do echo -n "${1:i:1}"; done; echo Mar 24, 2014 at 19:02
2

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
2
  • 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. Mar 24, 2014 at 19:55
  • Unlike rev, this can also be used on OpenWRT.
    – bk138
    Mar 27, 2015 at 0:30
0

Try Perl:

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

Source: Perl one-liners

0

Given:

$ echo "$s"
12345
6789

Here is a Ruby that reverses each line of input:

$ echo "$s" | ruby -lpe '$_=$_.reverse'
54321
9876
-3
s=""
for i in $*
do
    s="$i $s";
done
for j in $s
do
    echo $j
done
2
  • 2
    How is that answering the question? May 23, 2014 at 12:37
  • 1
    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 May 23, 2014 at 15:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .