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?
3 Answers
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
A simple python thing, just for the giggles:
$ cat revline.py #!/usr/bin/env python # Origin: https://superuser.com/a/1623168/346288 import sys line=sys.stdin.readline() while len(line)>0: print( (line.strip('\r\n '))[-1::-1] ) line=sys.stdin.readline()
... and the output when run on itself:
$ ./revline.py < revline.py nohtyp vne/nib/rsu/!# 882643/8613261/a/moc.resurepus//:sptth :nigirO # sys tropmi )(enildaer.nidts.sys=enil :0>)enil(nel elihw ) ]1-::1-[))' n\r\'(pirts.enil( (tnirp )(enildaer.nidts.sys=enil $
Oh, btw: ln -s /usr/bin/python3 $HOME/bin/python
- assuming you have $HOME/bin
in your $PATH
- will make your life easier, assuming your python scripts are aware of python3
NOTE: reverses line content, but not the order of lines.