A symbol that replaces any character or string of characters as a parameter in a CLI command

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.

$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 tabs
The 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 parenthesis
You 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 -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

$ echo "Welcome    To    GeeksforGeeks" | tr -s [:space:] ' '

Output:

Welcome To GeeksforGeeks

5. 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.

$ 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 option
You 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

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 . 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.

A symbol that replaces any character or string of characters as a parameter in a CLI command

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.

A symbol that replaces any character or string of characters as a parameter in a CLI command

answered May 20, 2010 at 5:27

jkasnickijkasnicki

5,1961 gold badge14 silver badges9 bronze badges

6

You might find this link helpful: http://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_LMN

Notice that the +(pattern) pattern requires extended pattern matching, turned on with

shopt -s extglob

Alternatively, with the -s ("squeeze") option of tr:

$ tr -s xyz _ <<< "$var"
A_BC_DEF_LMN

answered Jan 2, 2017 at 6:03

A symbol that replaces any character or string of characters as a parameter in a CLI command

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

A symbol that replaces any character or string of characters as a parameter in a CLI command

1

What is a CLI explain with example?

CLI is a command line program that accepts text input to execute operating system functions. In the 1960s, using only computer terminals, this was the only way to interact with computers. In the 1970s an 1980s, command line input was commonly used by Unix systems and PC systems like MS-DOS and Apple DOS.

What does CLI computer term stand for?

A command-line interface (CLI) is a text-based user interface (UI) used to run programs, manage computer files and interact with the computer. Command-line interfaces are also called command-line user interfaces, console user interfaces and character user interfaces.

What is a command line tool?

Command line tools are scripts, programs, and libraries that have been created with a unique purpose, typically to solve a problem that the creator of that particular tool had himself.

How does command line interface work?

Abbreviated as CLI, a Command Line Interface connects a user to a computer program or operating system. Through the CLI, users interact with a system or application by typing in text (commands). The command is typed on a specific line following a visual prompt from the computer.