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
1. How to convert lower case to upper case
To convert from lower case to upper case the predefined sets in tr can be used.
Output:
WELCOME TO GeeksforGeeks$cat greekfile | tr “[a-z]” “[A-Z]”Output:
WELCOME TO GEEKSFORGEEKSor
$cat geekfile | tr “[:lower:]” “[:upper:]”Output:
WELCOME TO GEEKSFORGEEKS2. How to translate white-space to tabs
The following command will translate all the white-space to tabs
Output:
Welcome To GeeksforGeeks3. How to translate braces into parenthesis
You can also translate from and to a file. In this example we will translate braces in a file with parenthesis.
Output:
{WELCOME TO} GeeksforGeeks$ tr '{}' '()' newfile.txtOutput:
(WELCOME TO) GeeksforGeeksThe 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 -s
To 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
Output:
Welcome To GeeksforGeeks5. How to delete specified characters using -d option
To delete specific characters use the -d option.This option
deletes characters in the first set specified.
Output:
elcome To GeeksforGeeks6. To remove all the digits from the string, use
$ echo "my ID is 73535" | tr -d [:digit:]Output:
my ID is7. How to complement the sets using -c option
You can complement the SET1 using -c option. For example, to remove all characters except digits, you can use the following.
Output:
73535This 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 . 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.
401
New! Save questions or answers and organize your favorite content.
Learn more.
I have a string like AxxBCyyyDEFzzLMN and I want to replace all the occurrences of x, y, and z with _.
How can I achieve this?
I know that echo "$string" | tr 'x' '_' | tr 'y' '_' would work, but I want to do that in one go, without using pipes.
Benjamin W.
41k16 gold badges100 silver badges106 bronze badges
asked May 20, 2010 at 5:18
2
echo "$string" | tr xyz _
would replace each occurrence of x, y, or z with _, giving A__BC___DEF__LMN in your example.
echo "$string" | sed -r 's/[xyz]+/_/g'would replace repeating occurrences of x, y, or z with a single _, giving A_BC_DEF_LMN in your example.
answered May 20, 2010 at 5:27
jkasnickijkasnicki
5,1961 gold badge14 silver badges9 bronze badges
6
You might find this link helpful: //tldp.org/LDP/abs/html/string-manipulation.html.
In general, to replace the first match of $substring with $replacement:
${string/substring/replacement}To replace all matches of $substring with $replacement:
${string//substring/replacement}EDIT: Note that this applies to a variable named $string.
edddd
4032 silver badges14 bronze badges
answered Dec 9, 2014 at 0:14
Dylan DanielsDylan Daniels
1,9531 gold badge11 silver badges8 bronze badges
3
Here is a solution with shell parameter expansion that replaces multiple contiguous occurrences with a single _:
$ var=AxxBCyyyDEFzzLMN $ echo "${var//+([xyz])/_}" A_BC_DEF_LMNNotice that the +(pattern) pattern requires extended pattern matching, turned on with
shopt -s extglobAlternatively, with the -s ("squeeze") option of tr:
$ tr -s xyz _ <<< "$var" A_BC_DEF_LMNanswered Jan 2, 2017 at 6:03
Benjamin W.Benjamin W.
41k16 gold badges100 silver badges106 bronze badges
read filename ; sed -i 's/letter/newletter/g' "$filename" #letter
^use as many of these as you need, and you can make your own BASIC encryption
Nakilon
34.2k14 gold badges104 silver badges137 bronze badges
answered Jan 17, 2014 at 15:14
MichaelMichael
1091 silver badge2 bronze badges
0
echo 'I dont like PHP' | sed -r 's/I dont like/I love/g' ## Output: I love PHP
OR
echo 'I like PHP' | sed -r 's/like/love/g' ## Output: I love PHP
answered Sep 1 at 15:46
1