Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The use of tr commands to delete and replace text characters in Linux system

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "the use of tr commands to delete and replace text characters in the Linux system". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

By using tr, you can easily implement many of the most basic functions of sed. You can think of tr as a (extremely) simplified variant of sed: it can replace one character with another, or it can remove some characters altogether. You can also use it to remove duplicate characters. This is what all tr can do.

Tr is used to convert characters from standard input through replace or delete operations. Tr is mainly used to delete control characters in files or to convert characters. When using tr, you convert two strings: string 1 for queries and string 2 for various conversions. When tr is first executed, the characters in string 1 are mapped to characters in string 2, and then the conversion operation begins.

The format of the tr command with the most common options is:

The code is as follows:

Tr-c-d-s ["string1_to_translate_from"] ["string2_to_translate_to"]

< input-file 这里: -c 用字符串1中字符集的补集替换此字符集,要求字符集为ASCII。 -d 删除字符串1中所有输入字符。 -s 删除所有重复出现字符序列,只保留第一个;即将重复出现字符串压缩为一个字符串。 input-file是转换文件名。虽然可以使用其他格式输入,但这种格式最常用。 字符范围 指定字符串1或字符串2的内容时,只能使用单字符或字符串范围或列表。 [a-z] a-z内的字符组成的字符串。 [A-Z] A-Z内的字符组成的字符串。 [0-9] 数字串。 \octal 一个三位的八进制数,对应有效的ASCII字符。 [O*n] 表示字符O重复出现指定次数n。因此[O*2]匹配OO的字符串。 tr中特定控制字符的不同表达方式 速记符含义八进制方式 \a Ctrl-G 铃声\007 \b Ctrl-H 退格符\010 \f Ctrl-L 走行换页\014 \n Ctrl-J 新行\012 \r Ctrl-M 回车\015 \t Ctrl-I tab键\011 \v Ctrl-X \030 表达字符串的特殊序列 String1 和 String2 变量中所包含的字符串可以使用以下的约定来表示: 如果某个字符在 String1 中被指定过多次,则该字符就被转换成 String2 中为与 String1 中最后出现的字符相对应的字符。 如果由 String1 和 String2 指定的字符串长度不相同,则 tr 命令就会忽略较长一个字符串中的多余字符。 标志 退出状态 该命令返回以下出口值:

Example:

1. Replace "abc" in the file file with "xyz"

The code is as follows:

# cat file | tr "abc"xyz" > new_file

[note] here, every "a" letter that appears in file is replaced with the "x" letter, the "b" letter is replaced with the "y" letter, and the "c" letter is replaced with the "z" letter. Instead of replacing the string "abc" with the string "xyz".

2. Use the tr command to unify the case of letters

(lowercase-> uppercase)

The code is as follows:

# cat file | tr [amerz] [Amurz] > new_file

(uppercase-> lowercase)

The code is as follows:

# cat file | tr [Amurz] [Amurz] > new_file

3. Replace the number 0-9 in the file with amurj

The code is as follows:

# cat file | tr [0-9] [amurj] > new_file

4. Delete the "Snail" character in the file file

The code is as follows:

# cat file | tr-d "Snail" > new_file

[note] here, all the characters that appear in the file file will be deleted! Instead of tightly deleting the "Snail" string that appears.

5. Delete the newline'\ n' and tabulation'\ t 'characters in the file file

The code is as follows:

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

Invisible characters have to be represented by escape characters, which are uniform.

6. Delete the "consecutive" repeating letters and keep only the first one

The code is as follows:

# cat file | tr-s [a-zA-Z] > new_file

7. Delete blank lines

The code is as follows:

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

8. Delete the'^ M 'character "caused" in the Windows file

The code is as follows:

# cat file | tr-d "\ r" > new_file

Or

The code is as follows:

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

[note] here-s is followed by two parameters "\ r" and "\ n". Replace the former with the latter.

9. Replace tab character\ 011 with space character\ 040

The code is as follows:

# cat file | tr-s "\ 011"\ 040" > new_file

10. Replace the colon ":" in the path variable with the newline character "\ n"

The code is as follows:

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

To convert curly braces to parentheses, enter:

The code is as follows:

Tr'{}'()'

< textfile >

Newfile

This converts each {(left brace) to ((left braces) and each} (right braces) to (right braces). All other characters remain the same.

To convert curly braces to square braces, enter:

The code is as follows:

Tr'{}'\ []'

< textfile >

Newfile

This converts each {(left brace) to [(left bracket) and each} (right bracket) to] (right bracket). The left square bracket must be entered with a "\" (backslash) escape character.

To convert lowercase characters to uppercase, enter:

The code is as follows:

Tr'a murz''Amurz'

< textfile >

Newfile

14. To create a list of words in a file, enter:

The code is as follows:

Tr-cs'[: lower:] [: upper:]'[\ n *]'

< textfile >

Newfile

This converts the characters of each sequence (except uppercase and lowercase letters) to a single newline character. * (asterisk) causes the tr command to repeat the newline character enough times to make the second string as long as the first string.

15. To remove all empty characters from a file, enter:

The code is as follows:

Tr-d'\ 0'

< textfile >

Newfile

16. To replace one or more newlines in each sequence with separate newlines, enter:

The code is as follows:

Tr-s'\ n'

< textfile >

Newfile

Or

The code is as follows:

Tr-s'\ 012'

< textfile >

Newfile

17. If you want to "?" (question mark) to replace each non-printing character (except for valid control characters), enter:

The code is as follows:

Tr-c'[: print:] [: cntrl:]'[? *]'

< textfile >

Newfile

This scans files created in different locales to find characters that cannot be printed in the current locale.

18. To replace each character sequence in the character class with a single "#" character, enter:

The code is as follows:

Tr-s'[: space:]'[# *]'

This is the end of the content of "the use of tr commands to delete and replace text characters in the Linux system". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report