您的位置:首页 > 编程语言

github/gitlab 管理多个ssh key

2015-07-16 09:17 344 查看
可以把tr看作为一个简化的sed工具,tr表示为:translate。tr命令主要用于实现以下两个功能
替换操作的字符串转换。

删除操作的字符串转换,可以很容易的删除一些控制字符或者是空行。
tr命令能够实现的功能,都能够用sed命令来实现。但就具体的替换功能来说,tr用起来更容易,也比较简单。

一,命令格式
[html] view plaincopy
tr [option] ["string1"] ["string2"] < file

常用的选项有:
默认选项。就是没有任何选项的时候,tr默认为替换操作,就是将string1在文件中出现的字符替换为string2中的字符,这里要注意的是替换关系。
-c选项,用string1中字符的补集替换string1,这里的字符集为ASCII。
-d选项,删除文件中所有在string1中出现的字符。
-s选项,删除文件中重复并且在string1中出现的字符,只保留一个。
-c选项在使用时,只是将string1替换为现在的补集,如在使用

C代码






[html] view plaincopy

[root@localhost client]# echo "hello world,root,2012" | tr -c "0-9" "*"

*****************2012*

[html] view plaincopy
[root@localhost client]# echo "hello world,root,2012" | tr -c "0-9" "*"
*****************2012*

可以看出,我们使用0-9,添加-c选项后,会把0-9替换为其补集,这时补集自然不包含0-9,而包含很多其它的字符,接下来就把所有的其它字符都替换成*号,但不包含数字。
如果只需要替换数字的话:
C代码






[html] view plaincopy

[root@localhost client]# echo "hello world,root,2012" | tr "0-9" "*"

hello world,root,****

[html] view plaincopy
[root@localhost client]# echo "hello world,root,2012" | tr "0-9" "*"
hello world,root,****

二,字符串的取值范围
指定string或string2的内容时,只能使用单字符或字符串范围或列表。
[a-z] a-z内的字符组成的字符串。
[A-Z] A-Z内的字符组成的字符串。
[0-9] 数字串。
\octal 一个三位的八进制数,对应有效的ASCII字符。
[O*n] 表示字符O重复出现指定次数n。因此[O*2]匹配OO的字符串。

三,控制字符的不同表达方式
速记符 含义 八进制方式
\a Ctrl-G 铃声\007
\b Ctrl-H 退格符\010
\f Ctrl-L 走行换页\014
\n Ctrl-J 新行\012
\r Ctrl-M 回车\015
\t Ctrl-I tab键\011
\v Ctrl-X \030 注意这些控制字符,如果想在linux下输入,如我们可能需要输入^M这种字符,只需ctrl+V+M同时按下即可。

四,字符替换
这是tr的默认操作,先看下面的命令和输出
C代码






[html] view plaincopy

[root@localhost client]# echo "hello world" | tr "a-z" "A-Z"

HELLO WORLD

[root@localhost client]# echo "hello world" | tr "a-l" "A-Z"

HELLo worLD

[root@localhost client]# echo "hello world" | tr "a-z" "A-H"

HEHHH HHHHD

[html] view plaincopy
[root@localhost client]# echo "hello world" | tr "a-z" "A-Z"
HELLO WORLD
[root@localhost client]# echo "hello world" | tr "a-l" "A-Z"
HELLo worLD
[root@localhost client]# echo "hello world" | tr "a-z" "A-H"
HEHHH HHHHD

第一行输出就是将小写换成大写。
第二行输出将小写中的a-l分别换成A-L,而将小写中的l以后的字符都不替换。
第三行输出将小写中的a-h换成A-H,而h以后的字符都换成H,因为后者的替换空间没有前面的字符空间大,所以就重复后面的H,相当于后面的字符是A-HHH......HHHHH。
如果我们想要进行大小写转换,可以按下面的输入:
C代码






[html] view plaincopy

tr "a-z" "A-Z" < inputfile

[html] view plaincopy
tr "a-z" "A-Z" < inputfile

五,去除重复字符
这个时候,所用的选项是-s选项,如:
C代码






[html] view plaincopy

[root@localhost client]# echo "hello world,root" | tr -s "ao"

hello world,rot

[root@localhost client]# echo "hello world,root" | tr -s "lo"

helo world,rot

[root@localhost client]# echo "hello world,root" | tr -s "a-z"

helo world,rot

[root@localhost client]# echo "hello world,root" | tr -s "0-9"

hello world,root

[html] view plaincopy
[root@localhost client]# echo "hello world,root" | tr -s "ao"
hello world,rot
[root@localhost client]# echo "hello world,root" | tr -s "lo"
helo world,rot
[root@localhost client]# echo "hello world,root" | tr -s "a-z"
helo world,rot
[root@localhost client]# echo "hello world,root" | tr -s "0-9"
hello world,root

第一行表示将输入字符串中的包含在"ao"字符集中的重复字符去掉,只留一个。因为"hello world,root",只有o满足条件,所以将root变成rot,把中间的两个o变成一个。
第二行将hello和root两个字符都压缩了。
第三行表示将a-z中的除复字符都去掉。
第三行表示将字符串中的重复的且重复字符在0-9字符集中的字符去掉,这里没有。

如果我们想要去掉空行,可以这样操作:
[html] view plaincopy
tr -s "\n" < inputfile 或者 tr -s "\012" <inputfile // 这两个是一样的。
就是将重复的换行符去掉,只留一个。

六,删除字符
-d选项和-s选项类似,只不过-d选项会删除所有出现的字符。
C代码






[html] view plaincopy

[root@localhost client]# echo "hello world,root" | tr -d "a-h"

llo worl,root

[root@localhost client]# echo "hello world,root,2012" | tr -d "a-z"

,,2012

[root@localhost client]# echo "hello world,root,2012" | tr -d "0-9"

hello world,root,

[html] view plaincopy
[root@localhost client]# echo "hello world,root" | tr -d "a-h"
llo worl,root
[root@localhost client]# echo "hello world,root,2012" | tr -d "a-z"
,,2012
[root@localhost client]# echo "hello world,root,2012" | tr -d "0-9"
hello world,root,

来源:http://www.cnblogs.com/fukajg/archive/2012/09/28/2706575.html

用法:tr [选项]... SET1 [SET2]
从标准输入中替换、缩减和/或删除字符,并将结果写到标准输出。

-c, -C, --complement        首先补足SET1
-d, --delete            删除匹配SET1 的内容,并不作替换
-s, --squeeze-repeats    如果匹配于SET1 的字符在输入序列中存在连续的
重复,在替换时会被统一缩为一个字符的长度
-t, --truncate-set1        先将SET1 的长度截为和SET2 相等
--help        显示此帮助信息并退出
--version        显示版本信息并退出

SET 是一组字符串,一般都可按照字面含义理解。解析序列如下:

\NNN    八进制值为NNN 的字符(1 至3 个数位)
\\        反斜杠
\a        终端鸣响
\b        退格
\f        换页
\n        换行
\r        回车
\t        水平制表符
\v        垂直制表符
字符1-字符2    从字符1 到字符2 的升序递增过程中经历的所有字符
[字符*]    在SET2 中适用,指定字符会被连续复制直到吻合设置1 的长度
[字符*次数]    对字符执行指定次数的复制,若次数以 0 开头则被视为八进制数
[:alnum:]    所有的字母和数字
[:alpha:]    所有的字母
[:blank:]    所有呈水平排列的空白字符
[:cntrl:]    所有的控制字符
[:digit:]    所有的数字
[:graph:]    所有的可打印字符,不包括空格
[:lower:]    所有的小写字母
[:print:]    所有的可打印字符,包括空格
[:punct:]    所有的标点字符
[:space:]    所有呈水平或垂直排列的空白字符
[:upper:]    所有的大写字母
[:xdigit:]    所有的十六进制数
[=字符=]    所有和指定字符相等的字符

仅在SET1 和SET2 都给出,同时没有-d 选项的时候才会进行替换。
仅在替换时才可能用到-t 选项。如果需要SET2 将被通过在末尾添加原来的末字符的方式
补充到同SET1 等长。SET2 中多余的字符将被省略。只有[:lower:] 和[:upper:]
以升序展开字符;在用于替换时的SET2 中以成对表示大小写转换。-s 作用于SET1,既不
替换也不删除,否则在替换或展开后使用SET2 缩减。

例子:
C代码






[root@localhost zhangy]# echo "TANK" |tr A-Z a-z #大写字母转小写

tank

[root@localhost zhangy]# echo 'tank zhang' | tr a-z A-Z #小写字线转大写

TANK ZHANG

[root@localhost zhangy]# cat aaa.txt #原文件

aaa

bbb

[root@localhost zhangy]# cat aaa.txt|tr 'a' 'c' #字母c替换字母a

ccc

bbb

[root@localhost zhangy]# cat aaa.txt|tr -d 'a' #删除所有字母a

bbb

[root@localhost zhangy]# cat aaa.txt|tr -d '\n\t' 删除文件file中出现的换行'\n'、制表'\t'字符

aaabbb

[root@localhost zhangy]# cat aaa.txt|tr -s [a-zA-Z] #删除重复的字母

a

b

[root@localhost zhangy]# cat aaa.txt|tr -s '\n' #删除空行

aaa

bbb

[root@localhost zhangy]# cat aaa.txt |tr -s '\011' '\040' #用空格符\040替换制表符\011

aaa

bbb

来源: http://l.51yip.com/search/tr
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: