
rev command reverses strings of text. This
command can operate either on provided text or a file, and it seems
deceptively simple. But like many command-line utilities, its real power
becomes apparent when you combine it with other commands.The
rev command is one of those simple Linux utilities
that, at first glance, appears to be something of an oddity. It performs
a single function: it reverses strings. And apart from being able to
print a quick help page (-h) and show you its version number (-V), it doesn’t accept any command-line options.So,
rev reverses strings, and that’s it? No variations
or options? Well, yes and no. Yes, it has no permutations, but no,
that’s hardly all. This tutorial shows you how to combine it for
powerful operations.When you use
rev as a building block in more complicated command sequences, it really starts to show its worth. rev is one of a group of commands (like tac and yes)
that are facilitators. It’s easier to appreciate their usefulness when
you see how they make the use of other commands more efficient.Using the rev Command
Used on the command line with no other parameters,rev
takes any typed input, reverses it, and then prints it in the terminal
window. It keeps doing this until you hit Ctrl+C to exit.rev

If you type some text and press Enter, it makes
rev print the string in reverse—unless you provide it with a palindrome, of course.
Passing Text to rev
You can useecho to pipe text to rev.echo one two three | rev

You can also use
rev to reverse the contents of an
entire file of text, line by line. In this example, we have a file
containing a list of filenames. The file is called “filelist.txt.”rev filelist.txt

Each line is read from the file, reversed, and then printed to the terminal window.
Combining rev with Other Commands
Here’s an example using piping of input that callsrev twice.This command strips the last character off the string of text. This could be useful to remove punctuation. We need to use the
cut command to strip the character.echo 'Remove punctuation.' | rev | cut -c 2- | rev

Let’s break that down.
echosends the string into the first call torev.revreverses the string and pipes it intocut.- The
-c(characters) option tellscutto return a sequence of characters from the string. - The
2-option tellscutto return the range of characters from character two until the end of the line. If a second number were provided, such as2-5, the range would be from characters two to five. No second number means “up to the end of the string.” - The reversed string—minus its first character—is passed to
revwhich reverses the string, so it’s back to its original order.
sed or awk, but this is an easier syntax.Separating the Last Word
We can use a similar trick to return the last word of the line.The command is similar to the last one: again, it uses
rev twice. The differences lie in the way the cut command is used to select portions of the text.echo 'Separate the last word' | rev | cut -d' ' -f1 | rev

Here’s the command breakdown:
echosends the string into the first call torev.revreverses the string and pipes it intocut.- The
-d' '(delimiter) option tellscutto return a sequence of characters delimited by a space. - The
-f1option tellscutto return the first section of the string not containing the delimiter. In other words, the first part of the sentence up to the first space. - The reversed first word is passed to
revwhich reverses the string, so it’s back to its original order.
Trimming Content From Files
Let’s say we have a file containing a list of filenames, and the filenames are in quotation marks. We want to remove the quotation marks from the filenames.Let’s look at the file:
less filelist.txt

The contents of the file are displayed for us in
less.
We can remove the punctuation from both ends of each line with the following command. This command uses both
rev and cut twice.rev filelist.txt | cut -c 2- | rev | cut -c 2-

The filenames are listed for us without the quotation marks.

The command breaks down like this:
revreverses the lines in the file and pipes them intocut.- The
-c(characters) option tellscutto return a sequence of characters from each line. - The
2-option tellscutto return the range of characters from character two until the end of each line. - The reversed strings, minus their first characters, are passed to
rev. revreverses the strings, so they’re back to their original order. They’re piped intocuta second time.- The
-c(characters) option tellscutto return a sequence of characters from each string. - The
2-option tellscutto return the range of characters from character two until the end of each line. This “hops over” the leading quotation mark, which is character one on each line.
A Lot of Piping
Here’s a command that returns a sorted list of every file extension in the current directory. It uses five distinct Linux commands.ls | rev | cut -d'.' -f1 | rev | sort | uniq

The process is straightforward:
lslists the files in the current directory. These are piped intorev.revreverses the filenames and pipes them intocut.cutreturns the first portion of each filename up to a delimiter. The-d'.'tellscutto use the period “.” as the delimiter. The portion of the reversed filenames up to the first period are the file extensions. These are piped intorev.revreverses the file extensions into their original order. They are piped intosort.sortsorts the file extensions and pipes the results intouniq.uniqreturns a single listing for each type of unique file extension. Note if there’s no file extension (such as for the makefile, and the directories Help and gc_help), the entire filename is listed.
-c (count) command-line option to the uniq command.ls | rev | cut -d'.' -f1 | rev | sort | uniq -c

We now get a sorted list of the different file types in the current directory with a count of each.
That’s a pretty nifty one-liner!
drawroF og ot drawkcaB gnioG
Sometimes you have to go backward to go forward. And you usually go forward fastest as part of a team.Add
rev to your repertoire of go-to commands, and you’ll soon be using it to simplify otherwise complicated command sequences.
Post a Comment