cheat:tr
# To replace : with a new line:
echo $PATH | tr ":" "\n"
echo $PATH | tr -t ":" \n
# To remove all occurance of "ab":
echo aabbcc | tr -d "ab" # ouput: cc
# To complement "aa":
# ("Complement" means to keep "aa", and replace all others with "x")
echo aabbccd | tr -c "aa" x # output: aaxxxxx (no newline)
# To complement "ab\n":
echo aabbccd | tr -c "ab\n" x #output: aabbxxx (with newline)
# To preserve all alpha(-c). ":-[:digit:] etc" will be translated to "\n". sequeeze mode:
echo $PATH | tr -cs "[:alpha:]" "\n"
# To convert an ordered list to an unordered list:
echo "1. /usr/bin\n2. /bin" | tr -cs " /[:alpha:]\n" "+"
# To remove all NULLs:
tr < file-with-nulls -d '\000' > file-without-nulls
tldr:tr
# tr
# Translate characters: run replacements based on single characters and character sets.
# More information: <https://www.gnu.org/software/coreutils/tr>.
# Replace all occurrences of a character in a file, and print the result:
tr find_character replace_character < path/to/file
# Replace all occurrences of a character from another command's output:
echo text | tr find_character replace_character
# Map each character of the first set to the corresponding character of the second set:
tr 'abcd' 'jkmn' < path/to/file
# Delete all occurrences of the specified set of characters from the input:
tr -d 'input_characters' < path/to/file
# Compress a series of identical characters to a single character:
tr -s 'input_characters' < path/to/file
# Translate the contents of a file to upper-case:
tr "[:lower:]" "[:upper:]" < path/to/file
# Strip out non-printable characters from a file:
tr -cd "[:print:]" < path/to/file
$
cheat.sh