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

Solution 1:

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

Solution 2:

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:

#!/bin/bash

length=${#1}
pointer=$((length-1))
while [ $pointer -ge 0  ];
do
    printf "%c" "${1:$pointer:1}"
    pointer=$((pointer-1))
done
printf "\n"

And it works like this:

$ ./reverse.sh "hello world"
dlrow olleh

To reverse each line in text file, we could do this:

#!/bin/bash
reverse_line(){
    length=${#1}
    pointer=$((length-1))
    while [ $pointer -ge 0  ];
    do
        printf "%c" "${1:$pointer:1}"
        pointer=$((pointer-1))
    done
    printf "\n"
}

while IFS= read -r line || [ -n "$line" ];
do
    reverse_line "$line"
done < "$1"

Test:

$ printf "hello world\nsecond line" > input.txt
$ ./reverse_file_lines.sh input.txt
dlrow olleh
enil dnoces

Of course, this isn't quite portable since ${var:start:offset} is bash-specific, so we could always resort to awk for portability:

awk '{for(i=length($0);i>0;i--) printf "%c",substr($0,i,1);print ""}'  input.txt

Solution 3:

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.

Solution 4:

The terminal mlterm has good features for right-to-left text. You can install it by running:

sudo apt install mlterm

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.

Screenshot