linux tr命令用于转换或删除文件中的字符。
tr指令从标准输入设备读取数据,经过字符串转译后,将结果输出到标准输出设备。
tr命令常用来替换,删除来自标准输入字符串中的字符。注意,操作的单位是字符,不是字符串。也就是说,不管是替换还是删除,都是针对字符串中某个字符的。比如tr命令不能很方便地实现字符串替换:把“hello world”变成“hello beijing”,但是可以很方便地把每一个大写字符变成小写字符,echo “hello world” | tr ‘[a-z]’ ‘[a-z]’, 最后输出“hello world”。
语法
tr [-cdst][--help][--version][第一字符集][第二字符集]
tr [option]…set1[set2]参数说明:
-c, –complement:反选设定字符。也就是符合 set1 的部份不做处理,不符合的剩余部份才进行转换
-d, –delete:删除指令字符
-s, –squeeze-repeats:缩减连续重复的字符成指定的单个字符
-t, –truncate-set1:削减 set1 指定范围,使之与 set2 设定长度相等
–help:显示程序用法信息
–version:显示程序本身的版本信息
字符集合的范围:
\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,代表你可自订的字符)
实例
将文件testfile中的小写字母全部转换成大写字母,此时,可使用如下命令:
cat testfile |tr a-z a-ztestfile文件中的内容如下:
$ cat testfile #testfile原来的内容
linux networks are becoming more and more common,
but scurity is often an overlooked
issue. unfortunately, in today’s environment all networks
are potential hacker targets,
fro0m tp-secret military research networks to small home lans.
linux network securty focuses on securing linux in a
networked environment, where the
security of the entire network needs to be considered
rather than just isolated machines.
it uses a mix of theory and practicl techniques to
teach administrators how to install and
use security applications, as well as how the
applcations work and why they are necesary.使用 tr 命令大小写转换后,得到如下输出结果:
$ cat testfile | tr a-z a-z #转换后的输出
linux networks are becoming more and more common, but scurity is often an overlooked
issue. unfortunately, in today’s environment all networks are potential hacker targets,
from tp-secret military research networks to small home lans.
linux network securty focuses on securing linux in a networked environment, where the
security of the entire network needs to be considered rather than just isolated machines.
it uses a mix of theory and practicl techniques to teach administrators how to install and
use security applications, as well as how the applcations work and why they are necesary.大小写转换,也可以通过[:lower][:upper]参数来实现。例如使用如下命令:
cat testfile |tr [:lower:] [:upper:]输出结果如下:
$ cat testfile | tr [:lower:] [:upper:] #转换后的输出
linux networks are becoming more and more common, but scurity is often an overlooked
issue. unfortunately, in today’s environment all networks are potential hacker targets,
from tp-secret military research networks to small home lans.
linux network securty focuses on securing linux in a networked environment, where the
security of the entire network needs to be considered rather than just isolated machines.
it uses a mix of theory and practicl techniques to teach administrators how to install and
use security applications, as well as how the applcations work and why they are necesary.删除windows文件“造成”的’^m’字符
cat $file_name | tr -d \r删除连续的字符
echo hhhhhh | tr -s [:alnum:]
新开的机子麻烦更换成的段阿里云服务器购买使用腾讯云服务器怎么开放ftp备案信息需要修改-备案平台是的程序被黑了现在被阻断了购买云服务器省钱哪个云盘服务器比较便宜好用啊nginx服务器绑定域名和设置根目录的方法