Linux命令之tr命令 您所在的位置:网站首页 linux中tr的用法 Linux命令之tr命令

Linux命令之tr命令

2024-06-29 14:35| 来源: 网络整理| 查看: 265

一、命令介绍

  tr 命令用于转换或删除文件中的字符。tr 指令从标准输入设备读取数据,执行转换(或者压缩、删除)处理,将结果输出到标准输出设备。

二、使用示例 0、示例文件

[root@test1 test]# cat 1.txt The string is used to describe the tr commcand! Who are you? [root@test1 test]# cat 2.txt This is Number 9 . This is Number 100 .

1、将文件1.txt字符转换全部为大写

[root@test1 test]# cat 1.txt | tr a-z A-Z THE STRING IS USED TO DESCRIBE THE TR COMMCAND! WHO ARE YOU? [root@test1 test]# cat 1.txt |tr [:lower:] [:upper:] THE STRING IS USED TO DESCRIBE THE TR COMMCAND! WHO ARE YOU?

2、字符全部转换为小写

[root@test1 test]# cat 1.txt | tr A-Z a-z the string is used to describe the tr commcand! who are you? [root@test1 test]# cat 1.txt |tr [:upper:] [:lower:] the string is used to describe the tr commcand! who are you?

3、删除数字

[root@test1 test]# cat 2.txt |tr -d 0-9 This is Number . This is Number . [root@test1 test]# cat 2.txt |tr -d 1-9 This is Number . This is Number 00 .

4、生成包含大小写字母、数字、特殊字符的字符串

[root@test1 test]# < /dev/urandom tr -dc a-zA-Z0-9-/ |head -c ${1:-12};echo L/f5Fwq6rFxh

5、删除重复的0

[root@test1 test]# cat 2.txt |tr -s 0 This is Number 9 . This is Number 10 .

6、把路径变量中的冒号":“,替换成换行符”\n"

[root@test1 test]# echo $PATH |tr -s “:” “\n” /usr/local/sbin /usr/local/bin /usr/sbin /usr/bin /root/bin

7、删除换行符

[root@test1 test]# cat 2.txt |tr -d “\n” This iss Number 9 .This is Number 100 .[root@test1 test]#

8、删除Windows文件“造成”的’^M’字符

[root@test1 test]# cat file | tr -d “\r”

9、转换指定字符串(相同长度)

[root@test1 test]# cat 1.txt |tr “Who” “Why” The string is used ty describe the tr cymmcand! Why are yyu?

三、使用语法及参数说明 1、使用语法 用法:tr [OPTION]… SET1 [SET2]用法:tr [参数选项] 字符集1 [字符集2] 字符集1:指定要转换或删除的原字符集。当执行转换操作时,必须使用参数“字符集2”指定转换的目标字符集。但执行删除操作时,不需要参数“字符集2”;字符集2:指定要转换成的目标字符集。 2、参数说明 -c, --complement:反选设定字符。也就是符合 SET1 的部份不做处理,不符合的剩余部份才进行转换-d, --delete:删除指令字符-s, --squeeze-repeats:缩减连续重复的字符成指定的单个字符-t, --truncate-set1:削减 SET1 指定范围,使之与 SET2 设定长度相等–help:显示程序用法信息–version:显示程序本身的版本信息 3、字符集合 \NNN 八进制值的字符 NNN (1 to 3 为八进制值的字符)\ 反斜杠\a Ctrl-G 铃声\b Ctrl-H 退格符\f Ctrl-L 走行换页\n Ctrl-J 新行\r Ctrl-M 回车\t Ctrl-I tab键\v Ctrl-X 水平制表符CHAR1-CHAR2 :字符范围从 CHAR1 到 CHAR2 的指定,范围的指定以 ASCII 码的次序为基础,只能由小到大,不能由大到小。[CHAR*] :这是 SET2 专用的设定,功能是重复指定的字符到与 SET1 相同长度为止[CHAR*REPEAT] :这也是 SET2 专用的设定,功能是重复指定的字符到设定的 REPEAT 次数为止(REPEAT 的数字采 8 进位制计算,以 0 为开始)[:alnum:] :所有字母字符与数字[:alpha:] :所有字母字符[:blank:] :所有水平空格[:cntrl:] :所有控制字符[:digit:] :所有数字[:graph:] :所有可打印的字符(不包含空格符)[:lower:] :所有小写字母[:print:] :所有可打印的字符(包含空格符)[:punct:] :所有标点字符[:space:] :所有水平与垂直空格符[:upper:] :所有大写字母[:xdigit:] :所有 16 进位制的数字[=CHAR=] :所有符合指定的字符(等号里的 CHAR,代表你可自订的字符)


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有