Tr
Table of Contents
tr
The basics of tr
- echo “This is a line of text” | tr ’a’ ’A’
- echo “This is a line of text” | tr ’aeio’ ’AEIO’
- echo “I’M A LEET HAXOR!” | tr ’AEO’ ’430’
- echo “This is a line of text” | tr -d ’aeio’
- echo “This is a line of text” | tr -d ’aeio ’
- echo “Thiis iis aa liinee oof teext” | tr -s ’aeio’
- echo “Thiis iis aa liinee oof teeeeext” | tr -s ’aeio’
- echo “Thiis iis aa liinee oof teeeeext” | tr -s ’[:lower:]’ ’[:upper:]’
- head /dev/urandom
- head /dev/urandom | tr -cd ’[:print:]’
- echo “my super secure password is 1234 ” | tr -cd [:digit:]
Compared to sed (or awk)
tr works on characters, not strings, and converts characters from one set to characters in another (as in tr ’A-Z’ ’a-z’ to lowercase input).
sed has a y command that is analogous to tr:
- echo “This is a line of text” | sed -e ’y/ai/AI/’
’y’ does not support character ranges a-z or classes [:alpha:] like tr does, nor the complement modes -c and -C.
- echo “This is a line of text” | sed -e ’y/[:lower:]/[:upper:]/’
Footer
Copyright © 2020-2021 Derek Taylor (DistroTube)
This page is licensed under a Creative Commons Attribution-NoDerivatives 4.0 International License (CC-BY-ND 4.0).
The source code for distro.tube can be found on GitLab. User-submitted contributions to the site are welcome, as long as the contributor agrees to license their submission with the CC-BY-ND 4.0 license.