Tuesday, June 20, 2017

Linux "tr" Command Examples





Translate upper case to lower
tr A-Z a-z < inputfile > outputfile
cat inputfile|tr A-Z a-z

Translate braces into parenthesis
tr '{}' '()' < inputfile > outputfile

Translate white-space to tabs
 echo "This is for testing" | tr [:space:] '\t'

Squeeze repetition of characters using -s
echo "This   is   for testing" | tr [:space:] '\t'

We can use -s option to squeeze the repetition of characters.
echo "This   is   for testing" | tr -s [:space:] '\t'

convert multiple continuous spaces with a single space
echo "This  is  for testing" | tr -s [:space:] ' '

Delete specified characters using -d option
echo "the geek stuff" | tr -d 't'

To remove all the digits from the string, use
echo "my username is 432234" | tr -d [:digit:]

Complement the sets using -c option
echo "my username is 432234" | tr -cd [:digit:]

Remove all non-printable character from a file
tr -cd [:print:] < file.txt

Join all the lines in a file into a single line
 tr -s '\n' ' ' < file.txt