文本处理工具命令——tr

一帮助说明

TR()                                       User Commands                                       TR()

NAME
tr - translate or delete characters SYNOPSIS
tr [OPTION]... SET1 [SET2] DESCRIPTION
Translate, squeeze, and/or delete characters from standard input, writing to standard output.

二常用选项

(一)删除字符或者分隔符

-d,--delete delete characters in SET1,do not translate删除指定字符,不做替换

-C,-C,--complement use the complement of SET1取删除指定字符的补集,相当r,reverse反转

示例——删除空白符

[root@centos73 ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 52403200 1451152 50952048 3% /
devtmpfs 487964 0 487964 0% /dev
tmpfs 498988 0 498988 0% /dev/shm
tmpfs 498988 14076 484912 3% /run
tmpfs 498988 0 498988 0% /sys/fs/cgroup
/dev/sr0 4364408 4364408 0 100% /mnt
/dev/sda3 20961280 87452 20873828 1% /app
/dev/sda1 1038336 126596 911740 13% /boot
tmpfs 99800 0 99800 0% /run/user/
[root@centos73 ~]# df  |  tr   -d  ' '
Filesystem1K-blocksUsedAvailableUse%Mountedon
/dev/sda2524032001451152509520483%/
devtmpfs48796404879640%/dev
tmpfs49898804989880%/dev/shm
tmpfs498988140764849123%/run
tmpfs49898804989880%/sys/fs/cgroup
/dev/sr0436440843644080100%/mnt
/dev/sda32096128087452208738281%/app
/dev/sda1103833612659691174013%/boot
tmpfs998000998000%/run/user/


示例——使用设备生成随机数

默认显示的很多随机数是乱码的,要对其进行过滤,取出前30个没有乱码的字符

取出数字加字母就够用了

[root@centos72 ~]# cat  /dev/urandom  |  tr  -cd  '[:alnum:]' |  head  -c30
GcCS7V3zdbxf7ULACZxyileWOkVbb4[root@centos72 ~]# cat /dev/urandom | tr -dc '[:alnum:]' | head -c30
4HRYAPkEmR9IU5DmwUCfPH5yFchmWk[root@centos72 ~]# cat /dev/urandom | tr -dc '[:alnum:]' | head -c30
Xai5M56ExMu3lPTk3uItOrqOIyrHBi[root@centos72 ~]# cat /dev/urandom | tr -dc '[:alnum:]' | head -c30
EM19jKg8elb0XaBZ5fBt6HlzvADj6V[root@centos72 ~]# cat /dev/urandom | tr -dc '[:alnum:]' | head -c30
2nGHfnxibhXymvYY50933w9IKZY2a1[root@centos72 ~]# cat /dev/urandom | tr -dc '[:alnum:]' | head -c30
FtlUPlUBAJqGuCF8D8mBWUA1Qy4mIE[root@centos72 ~]# cat /dev/urandom | tr -dc '[:alnum:]' | head -c30
pP7Qq0iqqv3ualzDdf6JYcyA7nnL0X[root@centos72 ~]# cat /dev/urandom | tr -dc '[:alnum:]' | head -c30
mcrr0qTGkAgFQ4Qp8wQYEOXkth6KBh[root@centos72 ~]#

示例——取6个字符,包含大小写字母

[root@centos71 ~]# tr -cd  'a-zA-Z0-9'< /dev/urandom  |  head  -c6
5PWu0c[root@centos71 ~]# tr -cd 'a-zA-Z0-9'< /dev/urandom | head -c6
eMHkgL[root@centos71 ~]# tr -cd 'a-zA-Z0-9'< /dev/urandom | head -c6
RlO3EL[root@centos71 ~]# tr -cd 'a-zA-Z0-9'< /dev/urandom | head -c6
pGlDTU[root@centos71 ~]# tr -cd 'a-zA-Z0-9'< /dev/urandom | head -c6
0WI1oX[root@centos71 ~]# tr -cd 'a-zA-Z0-9'< /dev/urandom | head -c6
pwho9R[root@centos71 ~]# tr -cd 'a-zA-Z0-9'< /dev/urandom | head -c6
IRe8FV[root@centos71 ~]#

(二)-s保留连续字符的第1个字符,删除其他字符


-s, --squeeze-repeats
              replace  each input sequence of a repeated character that is listed in SET1 with a sin?
              gle occurrence of that character
[root@centos73 ~]# df |grep "/sd" |tr -s  " "
/dev/sda2 % /
/dev/sda3 % /app
/dev/sda1 % /boot
[root@centos73 ~]# df |grep "/sd"
/dev/sda2 % /
/dev/sda3 % /app
/dev/sda1 % /boot

[root@centos73 ~]# cat /etc/issue
\S
Kernel \r on an \m [root@centos73 ~]# tr 'abcd' 'xyz' < /etc/issue
\S
Kernel \r on xn \m [root@centos73 ~]# cat /etc/issue
\S
Kernel \r on an \m

难点——tr的替换

情形一:替换前字符长度等于替换后字符长度

这种情况最简单,全部替换,并且一一对应

[root@centos71 test2]# echo  abcd  >  tr3.txt
[root@centos71 test2]# cat tr3.txt
abcd
[root@centos71 test2]# tr 'abcd' '1234' < tr3.txt

情形二:替换前字符长度大于替换后字符长度

前者多余的字符被后者最后一个字符替换

[root@centos71 test2]# echo  abcd  >>  tr.txt
[root@centos71 test2]# cat tr.txt
abcd
[root@centos71 test2]# tr 'abcd' '123'
^C
[root@centos71 test2]# tr 'abcd' '123' < tr.txt

情形三:替换前字符长度小于替换后字符长度

前者多余的字符保留

[root@centos71 test2]# echo   abcd  >  tr2.txt
[root@centos71 test2]# cat tr2.txt
abcd
[root@centos71 test2]# tr 'abc' '1234' < tr2.txt
123d

情形四:替换前后字符出现重复情况

替换为最后出现的字符

[root@centos71 test2]# echo   abcba  >  tr4.txt
[root@centos71 test2]# cat tr4.txt
abcba
[root@centos71 test2]# tr 'abcba' '123456' < tr4.txt


命令——tr的更多相关文章

  1. Linux命令-tr

    tr命令用于转换文本文件中的字符 [root@localhost test]# cat .txt abcdefg asdfoui asdfqer [root@localhost test]# cat ...

  2. linux 命令 — tr

    tr 对stdin字符进行替换.删除和压缩,基本形式 tr [options] set1 set2 将输入的字符串中的set1字符转换为set2中对应位置的字符 set1.set2表示字符集,如果se ...

  3. 【Linux】字符转换命令tr

    tr (traslate的缩写)可以用来删除一段信息当中的文字,或者是进行文字信息的替换! [root@www ~]# tr [-ds] SET1 ... 选项与参数: -d :删除信息当中的 SET ...

  4. linux基础命令---tr

    tr 删除或者更改文件中的字符串,这个指令一般需要两个字符集. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法       ...

  5. 转换和删除重复命令tr

    前几篇文章介绍了几个用于处理字符的命令和工具,然而在处理大小写转换.删除重复字符等任务时,这些命令处理起来相对较为麻烦.这里将简单介绍Linux下自带的tr命令,相对于其他命令而言,其语法较为简单,比 ...

  6. Linux Shell 命令--tr

    tr        用来从标准输入中通过替换或删除操作进行字符转换                -c或--complerment 取代所有不属于第一字符集的字符.                -d ...

  7. linux 实用命令 —— tr

    1. [:alnum:] tr -cd '[:alnum:]' -d:delete:-c:complement:-cd:删除后边的参数以外的: [:class:] [:alpah:]:字母,[:dig ...

  8. tr命令的使用

    tr是translate的简写,亦即翻译,但是遗憾的是,它不能翻译句子,只能翻译单个字符. 1 tr的工作原理是什么? 先记住一点,tr命令不接受指定的文件参数,而只是对标准输入进行翻译.好了,记住这 ...

  9. 【Linux基础】tr命令替换和删除字符

    1.tr命令 tr可以对来自标准输入的字符进行替换.压缩和删除,可以将一组字符变成另外一组字符.通过使用 tr,您可以非常容易地实现 sed 的许多最基本功能.您可以将 tr 看作为 sed 的(极其 ...

随机推荐

  1. springboot定时任务出错 Unexpected use of scheduler.

    java.lang.IllegalStateException: Unexpected use of scheduler. 在启动类加: @Bean public ThreadPoolTaskSche ...

  2. 013-Spring Boot web【二】静态资源、Servlet、Filter、listenter

    一.静态资源 1.1.webapp默认支持静态资源 在src/main/webapp下建立user.html默认支持访问 1.2.默认内置静态资源目录.可被直接访问 查看包:spring-boot-a ...

  3. SpringMvc @ModelAttribute 的用法

    参考:Spring 3.x 企业应用开发实战   第15章:SpringMvc  页码:532 ModelAttribute 从字面上解释就是模型的属性. 对于MVC框架来说是模型数据是最重要的,因为 ...

  4. MySQL 编码:utf8 与 utf8mb4,utf8mb4_unicode_ci 与 utf8mb4_general_ci

    参考:mysql字符集小结 utf8mb4 已成为 MySQL 8.0 的默认字符集,在MySQL 8.0.1及更高版本中将 utf8mb4_0900_ai_ci 作为默认排序规则. 新项目只考虑 u ...

  5. UDP信号驱动IO

    SIGIO信号 信号驱动式I/O不适用于TCP套接字, 因为产生的信号过于频繁且不能准确判断信号产生的原因. 设置信号驱动需把sockfd的非阻塞与信号驱动属性都打开 server sockfd单独提 ...

  6. SVG.JS 画弧线

    需求描述: 使用svg.js,绘制一个弧线.下图绿色弧线. 准备工作: 1.了解SVG Path中的A指令 详细文档,请戳这里 给定x半径.y半径后,经过指定的两点,可以有2个椭圆,因此两点间有2条弧 ...

  7. vuejs基础-MVVM结构

    Vue.js 基本代码 和 MVVM 之间的对应关系 处理过程: 每当用户进行业务处理时,如果需要进行业务处理,都会通过网络请求,去请求后端的服务器,此时,我们的这个请求,就会被后端的App.js监听 ...

  8. Spring MVC-学习笔记(2)DispatcherServlet、@Controller、@RequestMapping、处理方法参数类型、可返回类型、Model、ModelMap、ModelAndView

    1.前端控制器org.springframework.web.servlet.DispatcherServlet 所有的请求驱动都围绕这个DispatcherServlet来分派请求.springMV ...

  9. MySQL-第十二篇管理结果集

    1.ResultSet 2.可更新的结果集,使用ResultSet的updateRow()方法.

  10. ES6扩展运算符(三点符号), 解构

    http://www.cnblogs.com/chrischjh/p/4848934.html