قالب وردپرس درنا توس
Home / Tips and Tricks / Using the rev Command on Linux

Using the rev Command on Linux



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

Linux Command rev reverses text strings. This command can be executed either with supplied text or a file and seems to be deceptively simple. However, as with many command-line utilities, the actual performance becomes clear when you combine them with other commands.

The command rev is one of those simple Linux utilities that at first glance seem to be something of a curiosity. It performs a single function: it reverses strings. In addition to being able to print a quick help page ( -h ) and display the version number ( -V ), no command line options are accepted. [1

9659004] So, rev reverses strings, and that's it? No variations or options? Yes and no Yes, there are no permutations, but no, that's hardly all. This tutorial will show you how to combine it for powerful operations.

Using rev as a building block in more complex command sequences shows that it really pays off. rev belongs to a group of commands (such as tac and yes ) that are mediators. It's easier to gauge their usefulness by seeing how they make using other commands more efficient.

Using the rev Command

Used on the command line without any other parameters, rev accepts all input commands, reverses them, and then prints them in the terminal window. This will happen until you press Ctrl + C to exit.

  rev 

If you enter a text and press Enter, the string of rev will be printed in reverse order unless you provide it with a palindrome.

Text passed to rev

With echo you can forward text to rev .

  echo one two three | rev 

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

  rev filelist.txt 

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

Combining rev with other commands

Here is an example of using piping inputs that call . rev twice.

This command removes the last character from the text string. This can be useful for removing punctuation. We must use the command cut to remove the character.

  echo & # 39; remove punctuation & # 39 ;. | rev | cut -c 2- | rev 

Let's resolve that.

  • echo sends the string in the first call to rev .
  • rev reverses the string and forwards it to cut .
  • The option -c (characters) instructs cut to return a sequence of characters from the string. [196909023] The Option 2- instructs cut to return the range of characters from two characters to the end of the line. If a second number were specified, e.g. For example, 2-5 would be the range between two and five characters. No second number means "until the end of the string".
  • The inverse string minus the first character is passed to rev which reverses the string. 19659027] Since we truncated the first character of the reverse string, we truncated the last character of the original string. Yes, you could do this with sed or awk but this is a simpler syntax.

    Disconnecting the Last Word

    We can use a similar trick to return the last word of the line.

    The command is similar to the last: Again, rev is used twice. The differences lie in the way in which parts of the text are selected using the command cut .

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

    Here's the command breakdown:

    • echo sends the string in the first call to rev .
    • rev reverses the string and forwards it to cut .
    • The option -d & # 39; & # 39; (separator) instructs cut to return a sequence of characters delimited by a space.
    • -f1 The 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 up to the first space.
    • The inverse first word is passed to rev reversing the string so that the original order is restored.

    We extracted the first word of the reverse string and truncated the last word of the original string. The last word of the sentence was "word" and was printed out for us.

    Cropping content from files

    Let's say we have a file with a list of filenames and the filenames are in quotation marks. We want to remove the quotation marks from the file names.

    Let's look at the file:

      less filelist.txt 

    The content of the file will be less for us .

     Contents of the filename.txt file in less in a terminal window. [19659004] With the following command we can remove the punctuation at both ends of each line. This command uses both rev and cut twice.

      rev filelist.txt | cut -c 2- | rev | Section -c 2- 

    The file names are listed for us without quotation marks.

     Filenames without quotation marks in a terminal window.

    The command is split as follows: [19659022] rev reverses the lines in the file and forwards them to cut .

  • The option -c (character) instructs cut to return a string from each line.
  • The option 2- instructs cut to return the range of characters from two characters to the end of each line.
  • The Inverted Strings Without the first characters, the strings are renumbered .
  • rev. so that they appear in the original order again. They are conducted a second time in cut .
  • The option -c (character) instructs cut to return a sequence of characters from each string.
  • The option 2- instructs cut to return the range of characters from two characters to the end of each line. This jumps over the leading quotation mark, which is the first character in each line.

Much Piping

Here is a command that returns a sorted list of all file extensions in the current directory. There are five different Linux commands used.

  ls | rev | cut -d & # 39;. & # 39; -f1 | rev | sort | uniq 

The process is straightforward:

  • ls lists the files in the current directory. These are forwarded in rev .
  • rev reverses the filenames and forwards them to cut .
  • cut returns the first part of each filename as far as a delimiter. The -d & # 39; & # 39;. assigns cut the dot "." To use as a limiter. The portion of the reversed filenames to the first dot are the file extensions. These are forwarded in rev .
  • rev returns the file extensions to their original order. They are forwarded to sort .
  • sort sorts the file extensions and forwards the results to uniq .
  • uniq returns a single listing of the unique file extension for each type. If there is no file extension (for example, for the Makefile and the Help and gc_help directories), the entire file name is listed.

Finally, add the character -c (count) command-line option for the uniq command.

  ls | rev | cut -d & # 39;. & # 39; -f1 | rev | sort | uniq -c 

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

That's a pretty clever one-liners!

drawroF og ot drawkcaB gnioG [19659008] Sometimes you have to go back to go forward. And usually, as part of a team, you move forward the fastest.

Add rev to your repertoire of forwarding commands, and you'll soon be using it to simplify otherwise complicated command sequences.




Source link