Linux common commands: tr command

  The tr command implements the character conversion function, and its function is similar to the sed command, but the tr command is simpler than the sed command. That is to say, the functions that the tr command can achieve, the sed command can achieve. Nevertheless, the tr command is still a common command for processing text under Linux systems.

1. Command format:

  tr [options]... SET1 SET2

2. Command function:

  Replace, reduce, and/or delete characters from standard input, and write the result to standard output.

3. Common parameters:

-c, -C, --complement Replace with strings in set 1, requiring character set to be ASCII.
-d, --delete delete the content matching S ET1 without replacing
-s, --squeeze-repeats If the characters matching SET1 have consecutive repetitions in the input sequence, they will be uniformly shortened to one character when replacing Length
-t, --truncate-set1 First truncate the length of SET1 to be equal to SET2
    --help display this help information and exit
    --version display version information and exit

---------------------------------------------------------------------------------

SET is a set of strings, generally understood literally. The parsing sequence is as follows:


  \NNN Octal value of NNN character (1 to 3 digits)
  \\ backslash
  \a terminal beep
  \b backspace
  \f form feed
  \n line feed
  \r carriage return
  \t horizontal tab
  \v vertical Tab
  character 1-character 2 All characters experienced in the ascending process from character 1 to character 2
  [character *] Applicable in SET2, the specified character will be copied continuously until it matches the length of set 1
  [character * number of times] pairs of characters Perform the specified number of copies, if the number starts with 0, it is treated as an octal number
  [:alnum:] All letters and numbers
  [:alpha:] All letters
  [:blank:] All horizontal whitespace characters
  [:cntrl :] All control characters
  [:digit:] All digits
  [:graph:] All printable characters, excluding spaces
  [:lower:] All lowercase letters
  [:print:] All printable characters, including spaces
  [:punct:] All punctuation characters
  [:space:] All horizontal or vertical whitespace characters
  [:upper:] All uppercase letters
  [:xdigit:] all hexadecimal numbers
  [=characters=] All characters equal to the specified character

will only be replaced if both SET1 and SET2 are given and the -d option is not present.

The -t option is only possible for substitution. If needed, SET2 will be supplemented to the same length as SET1 by adding the original last character at the end. Extra characters in SET2 will be omitted.

Only [:lower:] and [:upper:] expand characters in ascending order;

Case conversion is represented as a pair in SET2 for substitution. -s Act on SET1, neither
replace nor delete, otherwise use SET2 to reduce after replacement or expansion.

 

4. Common formats:

  tr -c -d -s ["string1_to_translate_from"] ["string2_to_translate_to"] < input-file

here:

  • -c Replace this character set with the complement of the character set in string 1, requiring the character set to be ASCII.
  • -d Delete all input characters in string 1.
  • -s Remove all repeated character sequences, keeping only the first; that is, compress the repeated string into a single string.
  • input-file is the transform filename. Although other formats can be used for input, this format is the most commonly used.

Character range:

  • When specifying the contents of String 1 or String 2, only a single character or a range of strings or a list can be used.
  • [az] A string of characters within az.
  • [AZ] A string of characters within AZ.
  • [0-9] Numeric string.
  • \octal A three-digit octal number corresponding to a valid ASCII character.
  • [O*n] means that the character O is repeated a specified number of times n. So [O*2] matches the string of OO.

5. Common examples:

Example 1: Replace occurrences of "abc" in file file with "xyz"

1 [root@Gin scripts]# cat t.txt
2 abc
3 [root@Gin scripts]# cat t.txt |tr "abc" "xyz"
4 xyz
5 [root@Gin scripts]# cat t.txt
6 abc

 [Note] Here, all "a" letters that appear in the t.txt file are replaced with "x" letters, "b" letters are replaced with "y" letters, and "c" letters are replaced with "z" letters. instead of replacing the string "abc" with the string "xyz". The replacement here does not modify the source file.

 

Example 2: Using the tr command to "unify" letter case

1 [root@Gin scripts]# cat file
2 abc
3 [root@Gin scripts]# cat file|tr [a-z] [A-Z]
4 ABC

 

Note: To convert from uppercase to lowercase, you only need to change the position of the parameters after tr!

 

Example 3: Replace the numbers 0-9 in the file with aj

1 [root@Gin scripts]# cat file|tr [0-9] [a-j]
2 abcdefghij

 

 

Example 4: Remove "Snail" characters appearing in file file

[root@Gin scripts]# cat file
what is Snail
[root@Gin scripts]# cat file|tr -d "Snail"
wht s
[root@Gin scripts]# cat file
what is Snail

[Note] Here, all 'S', 'n', 'a', 'i', 'l' characters that appear in the file will be deleted! Instead of just deleting the occurrences of the "Snail" string.

 

Example 5: Delete the newline '\n' and tab '\t' characters appearing in the file file

# cat file | tr -d "\n\t"

 

 

Example 6: Remove blank lines

1 # cat file | tr -s "\n" > new_file

 

 

Example 7: Remove the '^M' character "caused" by a Windows file

1 # cat file | tr -d "\r"
2 或者
3 # cat file | tr -s "\r" "\n"

 

[Note] Here -s is followed by two parameters "\r" and "\n", replace the former with the latter

 

Example 8: Replace tab \011 with space \040

1 # cat file | tr -s "\011" "\040"

 

 

Example 9: Replace the colon ":" in the path variable with a newline "\n"

1 # echo $PATH | tr -s ":" "\n"

 

 

Example 10: Practical application 1, encryption and decryption:

1 [root@Gin scripts]# echo  12345 | tr  ' 0-9 '  ' 987654321 '   ## Encrypt
 2  87654 
3 [root@Gin scripts]# echo  87654 | tr  ' 987654321 '  ' 0-9 '   ## Decrypt
 4  12345

 

The above is a very interesting small example. Simple encryption and decryption are realized through mapping. To understand this example, you can then look down at ROT13 , a variant of Caesar encryption invented in ancient Rome.

[root@Gin scripts]# echo "hi,this is amosli" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
uv,guvf vf nzbfyv
[root@Gin scripts]# echo "uv,guvf vf nzbfyv" | tr 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm'
hi,this is amosli

 

ROT13 is the inverse of itself; that is, to restore ROT13, the same algorithm for encryption can be applied, so the same operation can be encrypted and decrypted again. Fantastic!

 

Example 11: Practical application 2, character set complement:

1 tr -c [set1] [set2]

 

The complement of set1 means that all characters not in set1 are included from this set. The most typical use is to remove all characters from the input text that are not in the complement. E.g:

1 [root@Gin scripts]# echo "hello 123 world " | tr -d -c '0-9 \n'
2  123

 

Here, the complement contains all characters except digits, space characters, and newlines, all of which are removed because -d was specified.

 

Example 12: Practical application 3, compressing characters with tr:

1 [root@Gin scripts]# echo "GNU is  not          UNIX . Recursicve right?" | tr -s  ' '
2 GNU is not UNIX . Recursicve right?

 

Use the -s parameter to compress repeated characters in a string. See another example:

 1 [root@Gin scripts]# cat sum.txt
 2 5
 3 4
 4 3
 5 5
 6 4
 7 3
 8 [root@Gin scripts]# cat sum.txt|echo $[ $(tr '\n' '+') 0 ]
 9 24
10 [root@Gin scripts]# cat sum.txt|echo $[ $(tr '\n' '+') ] 
11 -bash: 5+4+3+5+4+3+ : syntax error: operand expected (error token is "+ ")

 

Here, the addition operation is implemented using tr, tr '\n' '+' is replaced by a newline character with '+' and then connected, and finally an extra '+' is added to the number 0 to implement the addition.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325294477&siteId=291194637