What is the application of `rev` in real life?
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.
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:
ACGTGTCAT
TGCACAGTA
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
ATGACACGT
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.