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反转
示例——删除空白符
[[email protected] ~]# 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
[[email protected] ~]# 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个没有乱码的字符
取出数字加字母就够用了
[[email protected] ~]# cat /dev/urandom | tr -cd ‘[:alnum:]‘ | head -c30 GcCS7V3zdbxf7ULACZxyileWOkVbb4[[email protected] ~]# cat /dev/urandom | tr -dc ‘[:alnum:]‘ | head -c30 4HRYAPkEmR9IU5DmwUCfPH5yFchmWk[[email protected] ~]# cat /dev/urandom | tr -dc ‘[:alnum:]‘ | head -c30 Xai5M56ExMu3lPTk3uItOrqOIyrHBi[[email protected] ~]# cat /dev/urandom | tr -dc ‘[:alnum:]‘ | head -c30 EM19jKg8elb0XaBZ5fBt6HlzvADj6V[[email protected] ~]# cat /dev/urandom | tr -dc ‘[:alnum:]‘ | head -c30 2nGHfnxibhXymvYY50933w9IKZY2a1[[email protected] ~]# cat /dev/urandom | tr -dc ‘[:alnum:]‘ | head -c30 FtlUPlUBAJqGuCF8D8mBWUA1Qy4mIE[[email protected] ~]# cat /dev/urandom | tr -dc ‘[:alnum:]‘ | head -c30 pP7Qq0iqqv3ualzDdf6JYcyA7nnL0X[[email protected] ~]# cat /dev/urandom | tr -dc ‘[:alnum:]‘ | head -c30 mcrr0qTGkAgFQ4Qp8wQYEOXkth6KBh[[email protected] ~]#
示例——取6个字符,包含大小写字母
[[email protected] ~]# tr -cd ‘a-zA-Z0-9‘< /dev/urandom | head -c6 5PWu0c[[email protected] ~]# tr -cd ‘a-zA-Z0-9‘< /dev/urandom | head -c6 eMHkgL[[email protected] ~]# tr -cd ‘a-zA-Z0-9‘< /dev/urandom | head -c6 RlO3EL[[email protected] ~]# tr -cd ‘a-zA-Z0-9‘< /dev/urandom | head -c6 pGlDTU[[email protected] ~]# tr -cd ‘a-zA-Z0-9‘< /dev/urandom | head -c6 0WI1oX[[email protected] ~]# tr -cd ‘a-zA-Z0-9‘< /dev/urandom | head -c6 pwho9R[[email protected] ~]# tr -cd ‘a-zA-Z0-9‘< /dev/urandom | head -c6 IRe8FV[[email protected] ~]#
(二)-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
[[email protected] ~]# 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 [[email protected] ~]# df |grep "/sd" /dev/sda2 52403200 1450956 50952244 3% / /dev/sda3 20961280 87448 20873832 1% /app /dev/sda1 1038336 126596 911740 13% /boot
[[email protected] ~]# cat /etc/issue \S Kernel \r on an \m [[email protected] ~]# tr ‘abcd‘ ‘xyz‘ < /etc/issue \S Kernel \r on xn \m [[email protected] ~]# cat /etc/issue \S Kernel \r on an \m
原文地址:https://www.cnblogs.com/wang618/p/11039070.html
时间: 2024-10-22 14:17:43