tr command in Unix/Linux with examples

19 Feb, 2021
The tr command in UNIX is a command line utility for translating or deleting characters. It supports a range of transformations including uppercase to lowercase, squeezing repeating characters, deleting specific characters and basic find and replace. It can be used with UNIX pipes to support more complex translation. tr stands for translate.
Syntax :
$ tr [OPTION] SET1 [SET2]

Options
-c : complements the set of characters in string.i.e., operations apply to characters not in the given set-d : delete characters in the first set from the output.-s : replaces repeated characters listed in the set1 with single occurrence-t : truncates set1
Sample Commands
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
1. How to convert lower case to upper caseTo convert from lower case to upper case the predefined sets in tr can be used.
$cat greekfile
Output:
WELCOME TO 
GeeksforGeeks
$cat greekfile | tr “[a-z]” “[A-Z]”
Output:
WELCOME TO
GEEKSFORGEEKS
or
$cat geekfile | tr “[:lower:]” “[:upper:]”
Output:
WELCOME TO
GEEKSFORGEEKS
2. How to translate white-space to tabsThe following command will translate all the white-space to tabs
$ echo "Welcome To GeeksforGeeks" | tr [:space:] '\t'
Output:
Welcome    To    GeeksforGeeks    
3. How to translate braces into parenthesisYou can also translate from and to a file. In this example we will translate braces in a file with parenthesis.
$cat greekfile
Output:
 {WELCOME TO} 
GeeksforGeeks
$ tr '{}' '()'   newfile.txt
Output:
(WELCOME TO) 
GeeksforGeeks
The above command will read each character from “geekfile.txt”, translate if it is a brace, and write the output in “newfile.txt”.
4. How to use squeeze repetition of characters using -sTo squeeze repeat occurrences of characters specified in a set use the -s option. This removes repeated instances of a character.OR we can say that,you can convert multiple continuous spaces with a single space
$ echo "Welcome    To    GeeksforGeeks" | tr -s [:space:] ' '
Output:
Welcome To GeeksforGeeks
5. How to delete specified characters using -d optionTo delete specific characters use the -d option.This option deletes characters in the first set specified.
$ echo "Welcome To GeeksforGeeks" | tr -d 'w'
Output:
elcome To GeeksforGeeks
6. To remove all the digits from the string, use
$ echo "my ID is 73535" | tr -d [:digit:]
Output:
my ID is
7. How to complement the sets using -c optionYou can complement the SET1 using -c option. For example, to remove all characters except digits, you can use the following.
$ echo "my ID is 73535" | tr -cd [:digit:]
Output:
73535
Linux Tutorials | tr command | GeeksforGeeks - YouTubeGeeksforGeeks532K subscribersLinux Tutorials | tr command | GeeksforGeeksWatch laterShareCopy link28/36InfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.You're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmMore videosMore videosSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 4:47•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=iNWdEWWYo50" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Shivani Ghughtyal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
saurabhchauhan5
linux-command
Linux-text-processing-commands
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.