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.
The command you need is
rev
, e.g.:Of course this also works with files:
rev filename
instead ofrev <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 toecho
liketac
is tocat
, so to reverse the order of lines in a file:dessert's answer already covers the utilities that can be used for the purpose of reversing text. If we wanted to use shell-only tools, we could make use of
${variable:start:offset}
parameter expansion to reverse just a line first, then make a script that performs that on each line in text file.Line can be reversed like so:
And it works like this:
To reverse each line in text file, we could do this:
Test:
Of course, this isn't quite portable since
${var:start:offset}
is bash-specific, so we could always resort toawk
for portability:Take a look at the bicon tool which provides BiDi over terminal emulators.
Update:
GNOME Terminal 3.34 (in fact, VTE 0.58, and in turn all VTE-based emulators) support BiDi and automatically "flip" the order to RTL whenever needed.
There are some other emulators doing this too, e.g. Konsole and Mlterm.
For the rest, you can still use
bicon
.The terminal
mlterm
has good features for right-to-left text. You can install it by running:It will autodetect whether to display the line in right-to-left mode or in left-to-right mode, and whether to align the text to the right or the left.