What is the application of `rev` in real life?

According to https://www.geeksforgeeks.org/rev-command-in-linux-with-examples/

rev command in Linux is used to reverse the lines characterwise.


wolf@linux:~$ rev
Hello World!
!dlroW olleH

What is the example of application of rev in real life?

Why do we need reversed string?

Asked By: Wolf


The non-standard rev utility is useful in situations where it’s easier to express or do an operation from one direction of a string, but it’s the reverse of what you have.

For example, to get the last tab-delimited field from lines of text using cut (assuming the text arrives on standard input):

rev | cut -f 1 | rev

Since there is no way to express "the last field" to cut, it’s easier to reverse the lines and get the first field instead. One could obviously argue that using awk -F 't' '{ print $NF }' would be a better solution, but we don’t always think about the best solutions first.

The (currently) accepted answer to How to cut (select) a field from text line counting from the end? uses this approach with rev, while the runner-up answer shows alternative approaches.

Another example is to insert commas into large integers so that 12345678 becomes 12,345,678 (original digits in groups of three, from the right):

echo '12345678' | rev | sed -e 's/.../&,/g' -e 's/,$//' | rev

See also What do you use string reversal for? over on the SoftwareEngineering SE site for more examples.

Answered By: Kusalananda

As a specific example, rev is a useful tool to help reverse complement a DNA sequence, something often done in bioinformatics.

Remember that DNA is a double stranded polymer comprised of nucleotides. Generally DNA is modeled simply by the first letter of its nucleotides (A,C,G,T). If the "sequence" of one strand is known, the other strand is as well, since A always pairs with T, and C always pairs with G. Thus, a double stranded DNA sequence may be this:


Another thing about DNA and the two strands – they have directionality, and are often "read" from the 5′ end to the 3′ end. The strands have antiparallel directionality, and so are read in opposite directions – see the diagram below.

5'    ACGTGTCAT    3'
3'    TGCACAGTA    5'

Now – it is often useful to determine one strand given the other, and since sequences can be quite long (thousands or millions of nucleotides in length), this is done programmatically. One convenient way to do this is using rev (and tr):

echo ACGTGTCAT | tr ACGT TGCA | rev
Answered By: Scot

In the days before bidi-aware terminals (and even before UTF-8), it could have been used to display text files in Hebrew (Arabic was more involved because you needed to convert the characters into their presentation form), converting them from logical directionality to the physical one.

Answered By: Radovan Garabík

Here is an obscure but practical use for rev.

I once had a large tree of files I needed to archive. I planned to use tar and then gzip, in the usual way, but the resulting tarball was still too big — it wouldn’t fit on the disk I needed it to, or something.

But there was a lot of redundancy in the data — there were several directories containing large and nearly-identical files, although these directories were rather widely separated in the tree hierarchy. For example, there might have been things like


So I took the full list of files to be archived and ran it through

rev | sort | rev

This brought all the same-named files together, like this:


Then I used tar -T to archive my list of files in the regrouped order, then gzipped. My hope was that gzip’s algorithm would be able to do a better job of compressing the multiple copies of the big and similar files when they were next to each other, meaning that gzip would have its rolling dictionaries best optimized for reuse across the several copies.

To my pleasant surprise, this made a huge difference. I don’t remember the exact numbers, but I think I got about 3× better compression, and the final result fit on to the target disk easily.

Answered By: Steve Summit

If you have a stripboard layout as a text file (ASCII art), then rev flips it over to make a picture of the solder side to guide assembly.

Answered By: gatk555
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.