rev - macOS


Overview

rev reverses the lines of a file, or standard input if no file is specified. It’s often used for displaying text upside-down or in reverse chronological order.

Syntax

rev [options] [file]

Options/Flags

  • -h, –help : Display help and usage information.
  • -n, –number-lines : Prefix each reversed line with its original line number.
  • -s, –squeeze-blank : Remove empty lines from the output.

Examples

  • Display a file’s contents in reverse:

    rev filename
    
  • Reverse lines and add line numbers:

    rev -n filename
    
  • Reverse lines and remove empty lines:

    rev -s filename
    
  • Reverse standard input:

    echo "Hello World" | rev
    

Common Issues

  • File not found : Ensure the specified file path is valid.
  • Permission denied : Check if you have read access to the specified file.
  • Lines not reversed correctly : Verify that the input file doesn’t contain any non-text characters.

Integration

rev can be used with other commands for advanced tasks, such as:

  • head to display the first few lines of a reversed file:

    rev filename | head -5
    
  • tail to display the last few lines of a reversed file:

    rev filename | tail -5
    
  • sort to sort lines of a reversed file in reverse alphabetical order:

    rev filename | sort -r
    
  • tac : Another command that reverses lines of a file.
  • fold : Wraps long lines of text.
  • fmt : Formats text files.