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. Show 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 Sample Commands 1. How to convert lower case to upper case $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 $ echo "Welcome To GeeksforGeeks" | tr [:space:] '\t' Output: Welcome To GeeksforGeeks 3. How to translate braces into 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 $ echo "Welcome To GeeksforGeeks" | tr -s [:space:] ' ' Output: Welcome To GeeksforGeeks 5. How to delete specified characters using -d option $ 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 $ 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. I have a string like How can I achieve this? I know that
Benjamin W. 41k16 gold badges100 silver badges106 bronze badges asked May 20, 2010 at 5:18
2
would replace each occurrence of
would replace repeating occurrences of
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
To replace all matches of
EDIT: Note that this applies to a variable named
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
Notice that the
Alternatively, with the
answered Jan 2, 2017 at 6:03
Benjamin W.Benjamin W. 41k16 gold badges100 silver badges106 bronze badges
^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
OR
answered Sep 1 at 15:46
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.
|