How do I reverse the text in a file / stream in UNIX / Cygwin?

3

I have a block of text that I want to put in reverse-characer order (backmasking). Is there a simple way to do that via a Unix / Cygwin command-line?

rmiesen

Posted 2014-03-28T12:42:01.070

Reputation: 331

Answers

1

Use tac:

tac -r -s '.'

Examples

Reverse text from STDIN:
echo <YOUR TEXT GOES HERE> | tac -r -s '.'
Reverse text from a file:
tac -r -s '.' <FILENAME GOES HERE>

rmiesen

Posted 2014-03-28T12:42:01.070

Reputation: 331

0

Using rev is a bit simpler than tac (suggested by rmiesen). From the man page, the syntax is

rev [option] [file...]

If you omit the [file...], it'll read from stdin. So if you want to reverse text in a string, you can do

echo "some words backwards" | rev

which outputs

sdrawkcab sdrow emos

Or the alternative syntax

rev<<<"0123"

outputs

3210

cp.engr

Posted 2014-03-28T12:42:01.070

Reputation: 221