Ubuntu – Showing text file content right-to-left in the terminal

command linertlsoftware-recommendationtext processing

Is there any way for listing file contents in a shell window, such that characters are printed from right to left rather than as usual from the left to the right? or such that the BIDI algorithm steers line direction as per the text present?

This is relevant when working with text files belonging to RTL languages (as well as mixed LTR RTL documents) where you want to grep and search a lot without loading into an editor… editors don't like huge files…

Something around cat could be nice 🙂

In fact maybe I'll write something that just reverses each line and pipes to cat if nothing known exists.

Best Answer

The command you need is rev, e.g.:

$ rev <<<pa4080
0804ap
$ echo -e "pa4080_1\npa4080_2" | rev
1_0804ap
2_0804ap

Of course this also works with files:

$ >test echo -e "pa4080_1\npa4080_2"
$ <test rev # see annotation below
1_0804ap
2_0804ap

rev filename instead of rev <filename is possible but not preferable, as Stéphane Chazelas explains.

As part of util-linux rev is installed on every Ubuntu system by default.

This and alternative approaches can be found on HowTo: Reverse a String In Unix / Linux Shell? · nixCraft.


By the way: rev is to echo like tac is to cat, so to reverse the order of lines in a file:

$ >test echo -e "pa4080_1\npa4080_2"
$ <test tac
pa4080_2
pa4080_1
$ <test tac | rev
2_0804ap
1_0804ap