Easy methods to Use the rev Command on Linux

0
8


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

Linux’s rev command reverses strings of textual content. This command can function both on offered textual content or a file, and it appears deceptively easy. However like many command-line utilities, its actual energy turns into obvious if you mix it with different instructions.

The rev command is a kind of easy Linux utilities that, at first look, seems to be one thing of an oddity. It performs a single perform: it reverses strings. And aside from with the ability to print a fast assist web page (-h) and present you its model quantity (-V), it doesn’t settle for any command-line options.

So, rev reverses strings, and that’s it? No variations or choices? Properly, sure and no. Sure, it has no permutations, however no, that’s hardly all. This tutorial exhibits you learn how to mix it for highly effective operations.

Once you use rev as a constructing block in additional sophisticated command sequences, it actually begins to indicate its value. rev is certainly one of a bunch of instructions (like tac and yes) which can be facilitators. It’s simpler to understand their usefulness if you see how they make using different instructions extra environment friendly.

Utilizing the rev Command

Used on the command line with no different parameters, rev takes any typed enter, reverses it, after which prints it within the terminal window. It retains doing this till you hit Ctrl+C to exit.

rev

"rev" used with "stdin" in a terminal window.

In the event you sort some textual content and press Enter, it makes rev print the string in reverse—until you present it with a palindrome, after all.

"rev" with a palindrome in a terminal window.

Passing Textual content to rev

You need to use echo to pipe textual content to rev.

echo one two three | rev

"echo one two three | rev" in a terminal window.

It’s also possible to use rev to reverse the contents of a whole file of textual content, line by line. On this instance, we now have a file containing a listing of filenames. The file is named “filelist.txt.”

rev filelist.txt

"rev filelist.txt" in a terminal window.

Every line is learn from the file, reversed, after which printed to the terminal window.

Combining rev with Different Instructions

Right here’s an instance utilizing piping of enter that calls rev twice.

This command strips the final character off the string of textual content. This might be helpful to take away punctuation. We have to use the reduce command to strip the character.

echo 'Take away punctuation.' | rev | reduce -c 2- | rev

"echo 'Remove punctuation.' | rev | cut -c 2- | rev" in a terminal window.

Let’s break that down.

  • echo sends the string into the primary name to rev.
  • rev reverses the string and pipes it into reduce.
  • The -c (characters) choice tells reduce to return a sequence of characters from the string.
  • The 2- choice tells reduce to return the vary of characters from character two till the tip of the road. If a second quantity have been offered, similar to 2-5, the vary can be from characters two to 5. No second quantity means “as much as the tip of the string.”
  • The reversed string—minus its first character—is handed to rev which reverses the string, so it’s again to its authentic order.

As a result of we trimmed off the primary character of the reversed string, we trimmed off the final character of the authentic string. Sure, you possibly can do that with sed or awk, however that is a neater syntax.

Separating the Final Phrase

We will use the same trick to return the final phrase of the road.

The command is much like the final one: once more, it makes use of rev twice. The variations lie in the way in which the reduce command is used to pick out parts of the textual content.

echo 'Separate the final phrase' | rev | reduce -d' ' -f1 | rev

"echo 'Separate the last word' | rev | cut -d' ' -f1 | rev" in a terminal window.

Right here’s the command breakdown:

  • echo sends the string into the primary name to rev.
  • rev reverses the string and pipes it into reduce.
  • The -d' ' (delimiter) choice tells reduce to return a sequence of characters delimited by an area.
  • The -f1 choice tells reduce to return the primary part of the string not containing the delimiter. In different phrases, the primary a part of the sentence as much as the primary house.
  • The reversed first phrase is handed to rev which reverses the string, so it’s again to its authentic order.

As a result of we extracted the primary phrase of the reversed string, we trimmed off the final phrase of the authentic string. The final phrase of the sentence was “phrase,” and it’s printed out for us.

Trimming Content material From Information

Let’s say we now have a file containing a listing of filenames, and the filenames are in citation marks. We wish to take away the citation marks from the filenames.

Let’s take a look at the file:

much less filelist.txt

"less filenames.txt" in a terminal window.

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

Contents of filenames.txt in less in a terminal window.

We will take away the punctuation from each ends of every line with the next command. This command makes use of each rev and reduce twice.

rev filelist.txt | reduce -c 2- | rev | reduce -c 2-

"rev filelist.txt | cut -c 2- | rev | cut -c 2-" in a terminal window.

The filenames are listed for us with out the citation marks.

Filenames without quotation marks in a terminal window.

The command breaks down like this:

  • rev reverses the traces within the file and pipes them into reduce.
  • The -c (characters) choice tells reduce to return a sequence of characters from every line.
  • The 2- choice tells reduce to return the vary of characters from character two till the tip of every line.
  • The reversed strings, minus their first characters, are handed to rev.
  • rev reverses the strings, so that they’re again to their authentic order. They’re piped into reduce a second time.
  • The -c (characters) choice tells reduce to return a sequence of characters from every string.
  • The 2- choice tells reduce to return the vary of characters from character two till the tip of every line. This “hops over” the main citation mark, which is character one on every line.

A Lot of Piping

Right here’s a command that returns a sorted listing of each file extension within the present listing. It makes use of 5 distinct Linux instructions.

ls | rev | reduce -d'.' -f1 | rev | type | uniq

"ls | rev | cut -d'.' -f1 | rev | sort | uniq" in a terminal window.

The method is easy:

  • ls lists the recordsdata within the present listing. These are piped into rev.
  • rev reverses the filenames and pipes them into reduce.
  • reduce returns the primary portion of every filename as much as a delimiter. The -d'.' tells reduce to make use of the interval “.” because the delimiter. The portion of the reversed filenames as much as the primary interval are the file extensions. These are piped into rev.
  • rev reverses the file extensions into their authentic order. They’re piped into type.
  • type kinds the file extensions and pipes the outcomes into uniq.
  • uniq returns a single itemizing for every sort of distinctive file extension. Notice if there’s no file extension (similar to for the makefile, and the directories Assist and gc_help), your complete filename is listed.

To place a completion to it, add the -c (depend) command-line choice to the uniq command.

ls | rev | reduce -d'.' -f1 | rev | type | uniq -c

"ls | rev | cut -d'.' -f1 | rev | sort | uniq -c" in a terminal window.

We now get a sorted listing of the totally different file sorts within the present listing with a depend of every.

That’s a reasonably nifty one-liner!

drawroF og ot drawkcaB gnioG

Generally it’s a must to go backward to go ahead. And also you normally go ahead quickest as a part of a crew.

Add rev to your repertoire of go-to instructions, and also you’ll quickly be utilizing it to simplify in any other case sophisticated command sequences.





Source link

LEAVE A REPLY

Please enter your comment!
Please enter your name here