文本处理工具命令——tr


tr

一帮助说明

TR(1)                                       User Commands                                       TR(1)

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/0

 

[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/0
 
 







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

默认显示的很多随机数是乱码的,要对其进行过滤,取出前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 52403200 1450956 50952244 3% /
/dev/sda3 20961280 87448 20873832 1% /app
/dev/sda1 1038336 126596 911740 13% /boot
[root@centos73 ~]# df  |grep "/sd" 
/dev/sda2       52403200 1450956  50952244   3% /
/dev/sda3       20961280   87448  20873832   1% /app
/dev/sda1        1038336  126596    911740  13% /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
 






猜你喜欢

转载自www.cnblogs.com/wang618/p/11039070.html