01. Command Overview The tr command can replace, compress, and delete characters from standard input. It can transform one set of characters into another set of characters. It is often used to write beautiful single-line commands and is very powerful. The full English name of tr is “transform”, which means transformation. tr can only read data from standard input, so tr either redirects the input file to standard input or reads data from a pipe. Note: tr is similar to the sed command, but simpler than sed, so sed can achieve the same functions as tr. 02. Command format
03. Common options Replaces, reduces, and/or deletes characters from standard input and writes the results to standard output. -c, -C, --complement first complement SET1 SET is a set of strings, which can generally be understood literally. The parsing sequence is as follows: \NNN Character with octal value NNN (1 to 3 digits) Replacement is done only if both SET1 and SET2 are given, and no -d option is given. Character Range When specifying the contents of string1 or string2, you can only use single characters or string ranges or lists. [az] A string consisting of the characters in az. [AZ] A string consisting of the characters in AZ. \octal A three-digit octal number corresponding to a valid ASCII character. Different ways to express specific control characters in tr Shorthand meaning Octal mode \a Ctrl-G Ringtone\007 \f Ctrl-L Line feed\014 \r Ctrl-M Enter\015 \v Ctrl-X \030 04. Reference examples 4.1 Convert input characters from uppercase to lowercase [deng@localhost ~]$ echo "HELLO ITCAST" | tr 'AZ' 'az' hello itcast [deng@localhost ~]$ 'AZ' and 'a-z' are both sets. Sets can be customized. For example, 'ABD-}', 'bB.,', 'a-de-h', and 'a-c0-9' are all sets. You can use '\n', '\t', and other ASCII characters in the set. 4.2 Delete the numbers that appear [deng@localhost ~]$ echo "hello 1234 itcast 7890" | tr -d '0-9' hello itcast [deng@localhost ~]$ 4.3 Remove all characters from the input text that are not in the complement set [deng@localhost test]$ echo aabbcc..#dd2 */dk4 | tr -d -c '0-9 \n' twenty four [deng@localhost test]$ The complement contains the numbers 0-9, spaces, and newline characters \n, so they are not deleted, and all other characters are deleted. 4.4 Representing Repeated Characters as a Single Character [deng@localhost test]$ echo "helloooooooooo is heimamaaaaaaaaaaa" | tr -s 'oa' hello is heimama [deng@localhost test]$ 4.5 Using the Replace Operation to Perform the + Operation [deng@localhost test]$ echo 1 2 3 4 5 6 7 8 9 | xargs -n1 | echo $[ $(tr '\n' '+') 0 ] 45 [deng@localhost test]$ 4.6 Remove '^M' characters caused by Windows files [deng@localhost test]$ cat txt | tr -s '\r' '\n' > file [deng@localhost test]$ or [deng@localhost test]$ cat txt | tr -d '\r' > file 4.7 Convert lowercase letters to uppercase letters [deng@localhost test]$ echo "hello itcast" | tr '[:lower:]' '[:upper:]' HELLO ITCAST [deng@localhost test]$ 4.8 Replace line breaks with tabs [deng@localhost test]$ cat txt | tr '\n' '\t' 1111 1111 2222 2222 5555 [deng@localhost test]$ [deng@localhost test]$ This is the end of this article about how to use the Linux tr command. For more information about the Linux tr command, please search 123WORDPRESS.COM’s previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: A brief discussion on the role and working principle of key in Vue3
>>: MySQL dual-machine hot standby implementation solution [testable]
There are two types of Linux system time. (1) Cal...
Brief description Suitable for readers: Mobile de...
First, start MySQL in skip-grant-tables mode: mys...
1. Check and install pssh, yum list pssh 2. Becau...
This article shares a sharing sidebar implemented...
I have encountered the problem that MySQL can con...
Introduction This article records how to mount a ...
Environment Preparation Docker environment MySQL ...
Table of contents Multi-table join query Inner Jo...
The Docker container that has been running shows ...
Table of contents 1. Vue life cycle 2. Hook funct...
Table of contents 1. Install Vue scaffolding 2. C...
The party that creates a new connection is equiva...
<br />The content is reproduced from the Int...
Table of contents 1. Introduction 2. Main text 2....