16

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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
Wolf
  • 1,631

5 Answers5

54

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.

Kusalananda
  • 333,661
  • 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). – Stéphane Chazelas Dec 01 '20 at 11:27
  • 2
    (xcan would have been a command to print on canon laser printers from the era (early 80s). See can man page in v8) – Stéphane Chazelas Dec 01 '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. – RedGrittyBrick Dec 01 '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 01 '20 at 17:12
  • 2
    @RedGrittyBrick awk is a programming language. It doesn't go against the Unix philosophy any more than C or Perl do. – terdon Dec 03 '20 at 19:31
10

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
Scot
  • 211
  • 1
    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 02 '20 at 06: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 02 '20 at 07: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 02 '20 at 07: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 02 '20 at 07:20
3

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.

  • Interesting... do you have a rough timeframe? – qwr Dec 04 '20 at 05: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... – Radovan Garabík Dec 04 '20 at 07: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. – Radovan Garabík Dec 04 '20 at 07:42
3

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

a/b/c/d/efg
a/b/c/d/hij
a/b/c/d/klm
n/o/p/q/r/s/t/u/efg
n/o/p/q/r/s/t/u/hij
n/o/p/q/r/s/t/u/klm
v/w/efg
v/w/hij
v/w/klm

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:

a/b/c/d/efg
n/o/p/q/r/s/t/u/efg
v/w/efg
a/b/c/d/hij
n/o/p/q/r/s/t/u/hij
v/w/hij
a/b/c/d/klm
n/o/p/q/r/s/t/u/klm
v/w/klm

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.

0

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.