Text
rev
Reverse characters in each line of a file.
Additional Notes
rev reverses the order of characters on each line of input. The last character becomes the first, the second-to-last becomes the second, and so on. It operates line-by-line and outputs each reversed line.
rev is a simple utility in the util-linux package. It is useful for specialized text processing tasks like reversing columns, creating palindromes, or solving certain data extraction problems where reversing lines simplifies the work.
Syntax
rev [options] [file...]
Parameters
file: One or more files to process. If no file is given,revreads from standard input.
Common Options
-V,--version: Show version information.-h,--help: Show help and exit.
Examples
echo "hello" | rev
Reverse the word hello to olleh.
rev file.txt
Reverse every line in file.txt.
cat data.txt | rev
Reverse lines from standard input.
echo "123 456 789" | rev
Reverses to 987 654 321.
cut -d: -f1 /etc/passwd | rev
Reverse each username in the password file.
echo "racecar" | rev
Palindrome test: If the output matches the input, the word is a palindrome.
rev file.txt | sort | rev
Sort lines by their reversed content (equivalent to sorting from right to left).
paste <(cut -f1 data.txt) <(cut -f2 data.txt | rev)
Reverse only the second column in a tab-separated file.
Practical Notes
revis line-oriented. It does not reverse the order of lines, only the characters within each line.- Use
tac(reverse ofcat) to reverse the order of lines themselves. - For reversing fields (words), use
awk '{ for(i=NF; i>0; i--) printf "%s ", $i; printf "\n" }'. revpreserves trailing newlines; blank lines remain blank.- The command is simple but useful in one-liners and pipes where character-level reversal is needed.
- On some systems,
revis provided by thebsdmainutilsorutil-linuxpackage.