命令——tr
文本处理工具命令——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的更多相关文章
- Linux命令-tr
tr命令用于转换文本文件中的字符 [root@localhost test]# cat .txt abcdefg asdfoui asdfqer [root@localhost test]# cat ...
- linux 命令 — tr
tr 对stdin字符进行替换.删除和压缩,基本形式 tr [options] set1 set2 将输入的字符串中的set1字符转换为set2中对应位置的字符 set1.set2表示字符集,如果se ...
- 【Linux】字符转换命令tr
tr (traslate的缩写)可以用来删除一段信息当中的文字,或者是进行文字信息的替换! [root@www ~]# tr [-ds] SET1 ... 选项与参数: -d :删除信息当中的 SET ...
- linux基础命令---tr
tr 删除或者更改文件中的字符串,这个指令一般需要两个字符集. 此命令的适用范围:RedHat.RHEL.Ubuntu.CentOS.SUSE.openSUSE.Fedora. 1.语法 ...
- 转换和删除重复命令tr
前几篇文章介绍了几个用于处理字符的命令和工具,然而在处理大小写转换.删除重复字符等任务时,这些命令处理起来相对较为麻烦.这里将简单介绍Linux下自带的tr命令,相对于其他命令而言,其语法较为简单,比 ...
- Linux Shell 命令--tr
tr 用来从标准输入中通过替换或删除操作进行字符转换 -c或--complerment 取代所有不属于第一字符集的字符. -d ...
- linux 实用命令 —— tr
1. [:alnum:] tr -cd '[:alnum:]' -d:delete:-c:complement:-cd:删除后边的参数以外的: [:class:] [:alpah:]:字母,[:dig ...
- tr命令的使用
tr是translate的简写,亦即翻译,但是遗憾的是,它不能翻译句子,只能翻译单个字符. 1 tr的工作原理是什么? 先记住一点,tr命令不接受指定的文件参数,而只是对标准输入进行翻译.好了,记住这 ...
- 【Linux基础】tr命令替换和删除字符
1.tr命令 tr可以对来自标准输入的字符进行替换.压缩和删除,可以将一组字符变成另外一组字符.通过使用 tr,您可以非常容易地实现 sed 的许多最基本功能.您可以将 tr 看作为 sed 的(极其 ...
随机推荐
- win10蓝屏1
win10一直蓝屏. 事件查看里有系统错误提示 DCOM部分组件错误,表现为:应用程序-特定 权限设置并未向在应用程序容器 不可用 SID (不可用)中运行的地址 LocalHost (使用 LRPC ...
- nginx 入门 安装
.yum解决编译nginx所需的依赖包,之后你的nginx就不会报错了 yum install gcc patch libffi-devel python-devel zlib-devel bzip2 ...
- Hadoop实战内容摘记
Hadoop 开源分布式计算平台,前身是:Apache Nutch(爬虫),Lucene(中文搜索引擎)子项目之一. 以Hadoop分布式计算文件系统(Hadoop Distributed File ...
- 删除C:\Program Files (x86)\Common Files\baidu 等误装软件且正常模式删不掉的文件夹
---------方法一-------- C:\Program Files\Common Files\Baidu\BaiduProtect\1.1.0.26打开以上路径找到反注册程序uninst.ex ...
- django配置mysql报错 no model named "MySQLdb"
官网上面连接mysql数据库的参数很少,入了不少坑,一直排错和检查参数都没有问题,只能manage.py mirgrate 更新数据库的信息创建数据库的表. 很是郁闷.报了一大堆的错误,大概意思就是说 ...
- 解决django项目在ubuntu系统上无法安装mysqlclient
首先我的项目是django2.0,python环境是3.5. 我们在本地开发完django项目了,在本地运行是成功的,然后我们把django项目放到服务器上,运行的时候就出错了. 如图: 我们都知道, ...
- shell命令传参数(参数长度不定)
脚本 sudo echo "[mysqlMaster<$1>]" >> /home/admin/hostrecord count= ];do >> ...
- 从零开始学编程_第A001期_C语言HelloWorld详解
emmm,这是我的第一篇博客. 作为一个软件工程专业的学生,我希望自己能在编程方面有不错的成就,我们老师告诉我们学编程就要写博客,在写博客的过程中不断成长,结交朋友,所以我就开始尝试写专业相关的博客. ...
- python 合并字典/拼接字典
针对于python 3.5以上版本: 最好的最快的最优雅的方法是: result_dict = {**dict_1, **dict_2} 例如:( dict 代表 dictionary,也就是字典) ...
- 问题 B: 傻鸡抓大闸蟹
问题 B: 傻鸡抓大闸蟹 时间限制: 1 Sec 内存限制: 128 MB提交: 94 解决: 39[提交] [状态] [命题人:jsu_admin] 题目描述 背景又到了吃大闸蟹的季节,黄老师想 ...