linux tr命令
tr command is mainly used to translate or delete characters in bash. tr is not a complex but useful command. We will look at various usage scenarios of tr in this tutorial.
tr命令主要用于翻译或删除bash中的字符。 tr不是复杂但有用的命令。 在本教程中,我们将介绍tr的各种使用场景。
句法 (Syntax)
We will use the following syntax for tr
command.
我们将对tr
命令使用以下语法。
tr [OPTION]... SET1 [SET2]
帮帮我 (Help)
Help can get with --help
parameter like below.
可以使用--help
参数获得帮助,如下所示。
$ tr --help
人(Man)
The manpage of tr can be also viewed below.
tr的手册页也可以在下面查看。
$ man tr
将小写转换为大写(Convert Lowercase To Uppercase)
One of the most popular usages of tr is converting texts from lowercase to uppercase.
tr最受欢迎的用法之一是将文本从小写转换为大写。
$ tr [:lower:] [:upper:]
将大写转换为小写(Convert Uppercase To Lowercase)
Reverse application of Lowercase to uppercase is uppercase to lowercase. This can be implemented with the command below.
将小写字母反向转换为大写字母是将大写字母转换为小写字母。 可以使用以下命令来实现。
$ tr a-z A-Z
OR
要么
$ tr [:upper:] [:lower:]
将花括号翻译为括号(Translate Braces To Parenthesis)
We can translate braces into parenthesis like below. This can be applied to other characters too.
我们可以将花括号转换为括号,如下所示。 这也可以应用于其他字符。
$ tr '{}' '()'
将空格转换为制表符(Translate White-space To Tabs)
White spaces can be expressed like below and translated to the tabs with the following command.
空格可以如下表示,并使用以下命令转换为选项卡。
$ tr [:space:] '\t' < reverse_fruits.txt
删除指定字符(Delete Specified Characters)
Another useful feature is deleting specified characters from the given text. The character that will be deleted is specified with -d
parameter like below.
另一个有用的功能是从给定的文本中删除指定的字符。 使用-d
参数指定要删除的字符,如下所示。
$ tr -d 'T'
补语(Complement Characters)
The filtering is another feature of tr. Only Specified characters can be printed like below. In this example, we will only print numbers or digits with the following command.
过滤是tr的另一个功能。 只能打印指定的字符,如下所示。 在此示例中,我们将仅使用以下命令打印数字或数字。
$ echo "My ip address is 192.168.1.1" | tr -cd [:digit:]
删除所有不可打印的字符(Remove All Non-printable Characters)
Non-printable characters can be removed with the following command with-cd [:print:]
可以使用-cd [:print:]
使用以下命令来删除不可打印的字符
$ tr -cd [:print:] < reverse_fruits.txt
将多条线连接成单线(Join Multiple Line Into Single)
The following command will translate line endings into spaces and make the whole multi-line text a single line text.
以下命令将把行尾转换为空格,并使整个多行文本成为单行文本。
$ tr -s '\n' ' ' < reverse_fruits.txt
表达方式(Expressions)
All supported expressions can be found below.
可以在下面找到所有受支持的表达式。
CHAR1-CHAR2
all characters from CHAR1 to CHAR2 in ascending order
[CHAR*]
in SET2, copies of CHAR until length of SET1
[CHAR*REPEAT]
REPEAT copies of CHAR, REPEAT octal if starting with 0
[:alnum:]
all letters and digits
[:alpha:]
all letters
[:blank:]
all horizontal whitespace
[:cntrl:]
all control characters
[:digit:]
all digits
[:graph:]
all printable characters, not including space
[:lower:]
all lower case letters
[:print:]
all printable characters, including space
[:punct:]
all punctuation characters
[:space:]
all horizontal or vertical whitespace
[:upper:]
all upper case letters
[:xdigit:]
all hexadecimal digits
[=CHAR=]
all characters which are equivalent to CHAR
翻译自: https://www.poftut.com/linux-tr-command-tutorial-examples/
linux tr命令