Home > Text Processing & Search > rev

rev: Reverse a String

The rev command reads lines from standard input or a file, reverses the order of characters in each line, and outputs the result to standard output. It's a simple yet useful utility often used with pipes (|) to manipulate the output of other commands.

Overview

rev reverses each line of input text character by character. For example, 'hello' becomes 'olleh'. It can be used to reverse file content or for specific sorting tricks.

Key Features

  • Reverses the string of each line individually
  • Supports standard input and file input
  • Easily integrates with other commands via pipes
  • Very simple usage with few options

Main Options

As rev is a utility with a single purpose of reversing strings, it has almost no functional options. It primarily operates through input and output.

Basic Operation

Generated command:

Try combining the commands.

Description:

`rev` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Learn how to utilize the string reversal functionality through various examples of the rev command.

Reverse a String

echo "hello world" | rev

Reverses a string passed via the echo command using rev.

Reverse File Content

echo -e "apple\nbanana\norange" > fruits.txt
rev fruits.txt
rm fruits.txt

Reverses each line of a specified file and outputs it. (Creates a temporary file for the example.)

Sort Filenames by Reversed Order

touch file_a.txt file_b.txt file_c.txt
ls -1 | rev | sort | rev
rm file_a.txt file_b.txt file_c.txt

An advanced trick to get a list of files with ls -1, reverse them with rev, sort them with sort, and then reverse them back with rev to achieve a specific sorting pattern.

Tips & Precautions

Although rev is simple, it can perform powerful functions when combined with other commands.

Usage Tips

  • **Utilizing Pipes (|)**: rev takes standard input and outputs to standard output, making it very useful for immediately reversing the output of other commands.
  • **Log Analysis**: Can be used to read logs in reverse order for specific patterns or to sort specific fields in reverse.
  • **Sorting Tricks**: By reversing filenames or specific data fields, sorting them, and then reversing them back, you can implement specific sorting criteria that are difficult with standard sorting.

Precautions

  • **Line-by-Line Processing**: rev processes each line independently and does not reverse an entire text block across multiple lines. To reverse the entire text, you might need to combine it with other tools (e.g., reversing line order with `tac` and then applying `rev`).
  • **Character-based, Not Byte-based**: rev reverses based on characters. It correctly handles multi-byte characters (e.g., Korean).

Same category commands