How do I reverse the text in a file / stream in UNIX / Cygwin?
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?
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>
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