Tr command in Linux
tr is a command in Unix and Unix-like operating systems. It is an abbreviation of translate or transliterate, indicating its operation of replacing or removing specific characters in its input data set
- change one word to other
cat a.txt | tr o X
(i changer every o with capital x(X)
- change Upper to upper or lower to upper case
cat a.txt | tr [:upper:] [:lower:]
- change Upper to lower or upper to upper case
cat a.txt | tr [:lower:] [:upper:]
- you can use the following command to change all lower case letters to upper case in a file as shown
cat a.txt | tr [a-z] [A-Z]
- you can save the results written to stdout in a file for use the shell’s o/p
cat a.txt | tr [a-z] [A-Z] >ab.txt
cat ab.txt
- you can use the -d flag to delete characters
cat a.txt | tr -d ''
- If there are repeated characters in a sequence (for instance double spaces) in the text you are processing, you can use the -s option to squeeze the characters leaving only one occurrence of it.
cat a.txt | tr -s ''
- Related to the previous example, you can also translate multiple lines of words into a single sentence as shown.
tr "\n" " " < a.txt
Written By Sanjay Kumar
Comments
Post a Comment