linux tr命令_Linux tr命令教程及示例

linux tr命令

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
Help
Help
帮帮我

(Man)

The manpage of tr can be also viewed below.

tr的手册页也可以在下面查看。

$ man tr
Man
Man

将小写转换为大写(Convert Lowercase To Uppercase)

One of the most popular usages of tr is converting texts from lowercase to uppercase.

tr最受欢迎的用法之一是将文本从小写转换为大写。

$ tr [:lower:] [:upper:]
Convert Lower Case To Upper Case
Convert Lowercase To Uppercase
将小写转换为大写

将大写转换为小写(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:]
Convert Upper Case To Lower Case
Convert Uppercase To Lowercase
将大写转换为小写

将花括号翻译为括号(Translate Braces To Parenthesis)

We can translate braces into parenthesis like below. This can be applied to other characters too.

我们可以将花括号转换为括号,如下所示。 这也可以应用于其他字符。

$ tr '{}' '()'
Translate Braces To Parenthesis
Translate Braces To Parenthesis
将花括号翻译为括号

将空格转换为制表符(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
Translate White-space To Tabs
Translate White-space To Tabs
将空格转换为制表符

删除指定字符(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'
Delete Specified Characters
Delete Specified Characters
删除指定字符

补语(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:]
Complement Characters
Complement Characters
补语

删除所有不可打印的字符(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
Remove All Non-printable Characters
Remove All Non-printable Characters
删除所有不可打印的字符

将多条线连接成单线(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
Join Multiple Line Into Single
Join Multiple Line Into Single
将多条线连接成单线

表达方式(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
LEARN MORE  Python Standard Library
了解更多Python标准库

翻译自: https://www.poftut.com/linux-tr-command-tutorial-examples/

linux tr命令

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值