15

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

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

e.g.

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

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

Why do we need reversed string?

4
49

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.

5
  • 4
    In research Unix V8 (where rev comes from), the rev and revpag commands were documented in the same man page. With pr, troff and xcan (no idea what that one is) as SEE ALSO, suggesting document preparation and rendering contexts. Possibly it was to do with printing on the other side of pages with mirrored fonts (like for embossing or something). Dec 1 '20 at 11:27
  • 1
    (xcan would have been a command to print on canon laser printers from the era (early 80s). See can man page in v8) Dec 1 '20 at 11:35
  • This is an interesting example of the Unix toolbox philosophy - having many small text-oriented tools that each do one simple thing well and which can be combined to do more complex things. Programs like awk run against that philosophy somewhat. Dec 1 '20 at 15:12
  • 2
    @RedGrittyBrick You may think of awk as a tool for building tools though, much like you build tools for doing specific things by means of shell scripts and C programs etc. The short awk program I show in this answer could be seen as a tool for extracting the last field of a tab-delimited file. This could later be combined with other tools to do something else.
    – Kusalananda
    Dec 1 '20 at 17:12
  • 1
    @RedGrittyBrick awk is a programming language. It doesn't go against the Unix philosophy any more than C or Perl do.
    – terdon
    Dec 3 '20 at 19:31
9

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
4
  • Wouldn't it be significantly faster to use optimized tools for this kind of thing instead of shell tools which have some overhead?
    – qwr
    Dec 2 '20 at 6:52
  • Perhaps, though in my experience shell tools work quite well for bioinformatics data processing. Though my intent here was more to show how rev may be used.
    – Scot
    Dec 2 '20 at 7:10
  • Ok. Idk what scale of data people use for bioinformatics. For example it may be worthwhile to reverse a million character long string but not a file of several gigabytes, especially if a program can read in reverse and get away with not explicitly reversing a string.
    – qwr
    Dec 2 '20 at 7:12
  • 2
    The scale can vary widely depending on the application. I’ve used code like this frequently, though mainly for small scale. For example, I may have ~20 sequences of 8 nucleotides in length and I need the reverse complement of them.
    – Scot
    Dec 2 '20 at 7:20
2

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.

3
  • Interesting... do you have a rough timeframe?
    – qwr
    Dec 4 '20 at 5:19
  • @qwr definitely up until 2001 or so UTF-8 locales were not usable - before bash (and curses) implemented multibyte character support(let alone bidi....). I do not know when distributions switched by default to UTF-8, since I started to use an UTF-8 locale long before that, as soon as it becme practical. I remember trying to make a list of bidirectional terminal emulators and coming out with very few possibilities, and changelog shows this was around 2003... Dec 4 '20 at 7:42
  • @qwr @qwr ...and definitely many years after that your default terminal in your default distribution (unless it was Arabeyes or some other special localized one) would not support bidi, even if you could already install a usable alternative. Though, I'd say even today people sometimes need to work at linux text (or framebuffer these days) console, loading a Hebrew font is a matter of a simple seftont command, but forget bidi... and if you happen to need to read some Hebrew file on your server, rev would help. Though I imagine this is pretty rare. Dec 4 '20 at 7:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.