How to Use the rev Command on Linux

Linux terminal on an Ubuntu laptop.Fatmawati Achmad Zaenuri / Shutterstock

The rev command of Linux inverts the text strings. This command can operate either on supplied text or on 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 seem strange. It fulfills one function: it reverses the chains. And besides being able to print a quick help page (-h) and show you its version number (-V), it does not accept any command line options.

So, rev reverse the chains, and that's it? No variations or options? Well yes and no. Yes, there is no permutation, but no, it is hardly. This tutorial shows you how to combine it for powerful operations.

When you use rev as a building block in more complex command sequences, it really starts to show its full usefulness. rev is part of a group of commands (like tock and Yes) who are facilitators. It's easier to understand their usefulness when you see how they make the use of other commands more efficient.

Using the rev command

Used on the command line without any other parameters, rev takes a typed entry, the reverse, and then prints it into the terminal window. This continues until you press Ctrl + C to exit.

tower

If you type text and press Enter, the string is printed in the opposite direction by rev – unless you give it a palindrome, of course.

Passing Text to rev

You can use echo to direct the text to rev.

echo one two three | tower

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

rev filelist.txt

Each line is read from the file, inverted and printed in the terminal window.

Combination of rev with other orders

Here is an example using inlet piping that calls the diet twice.

This command deletes the last character of the text string. This could be useful for removing punctuation. We have to use the cut command for strip the character.

echo 'Remove punctuation.' | rev | cut -c 2- | tower

Let us break that down.

echo sends the string on the first call to rev.
rev reverse the chain and cut into slices.
The -c (characters) option tells cut to return a sequence of characters from the string.
Option 2 tells cut to return the range of characters between the second and the end of the line. If a second number was provided, such as 2-5, the range would be between two and five characters. No second number means "until the end of the chain".
The inverted string – minus its first character – is passed to rev which reverses it, so that it returns to its original order.

As we cut the first character of the inverted chain, we cut the last character of the original string. Yes, you can do it with sed or awk, but it's a simpler syntax.

Separate 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 control is used to select parts of the text.

echo 'Separate the last word & # 39; | rev | cut -d & # 39; -f1 | tower

Here is the distribution of orders:

echo sends the string on the first call to rev.
rev reverse the chain and cut into slices.
The option -d & # 39; (delimiter) tells cut to return a sequence of characters delimited by a space.
The -f1 option tells cut to return the first section of the string that does not contain the delimiter. In other words, the first part of the sentence to the first space.
The first inverted word is changed to rev, which reverses the string and returns to its original order.

As we extracted the first word from the reversed string, we cut the last word of the original string. The last word of the sentence was "word" and it was printed for us.

Cut the contents of the files

Say we have a file containing a list of file names and the file names are enclosed in quotation marks. We want to remove quotation marks from filenames.

Let's look at the file:

less filelist.txt

The contents of the file are displayed for us in less.

Content of filename.txt in minus in a terminal window.

We can remove the punctuation at 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-

Filenames are listed for us without quotation marks.

File names without quotation marks in a terminal window.

The command breaks down like this:

rev inverts the lines in the file and directs them in section.
The -c (characters) option tells cut to return a sequence of characters from each line.
Option 2 tells cut to return the range of characters between the second and the end of each line.
Inverted strings, minus their first characters, are changed to rev.
rev reverse the chains, so that they return to their original order. They are cut a second time.
The -c (characters) option tells cut to return a sequence of characters from each string.
Option 2 tells cut to return the range of characters between the second and the end of each line. This "jumps" on the quotation mark, character one on each line.

Plenty of piping

Here is a command that returns a sorted list of each file extension in the current directory. It uses five separate Linux commands.

ls | rev | cut-off. -f1 | rev | sort | uniq

The process is simple:

ls list the files in the current directory. These are channeled into rev.
rev reverses the file names and cuts them into pieces.
cut returns the first part of each file name up to a delimiter. The -d & # 39;. & # 39; tells Cut to use the dot "." as the delimiter. The part of the inverted file names up to the first period corresponds to the file extensions. These are channeled into rev.
rev reverse the file extensions in their original order. They are filtered.
sort sorts the file extensions and directs the results in uniq.
Uniq returns a unique list for each type of single file extension. Note that if there is no file extension (such as for makefile and the Help and gc_help directories), the full file name is listed.

To put the finishing touches, add the -c (count) command-line option to the single command.

ls | rev | cut-off. -f1 | rev | sort | uniq -c

We now get a sorted list of different types of files in the current directory with a number of each.

It's a nice one-liner!

drawrn og ot drawkcaB gnioG

Sometimes you have to go back and go forward. And you usually go faster in a team.

Add rev to your unmissable command directory and you'll use it soon to simplify otherwise complicated command sequences.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.