Linux tr 命令用于转换或删除文件中的字符。

Author: Bruce Liu Posted on: 2020-12-18 14:20:27 Visited: 252
tr [-cdst][--help][--version][第一字符集][第二字符集]  
tr [OPTION]…SET1[SET2] 

 

 

$ tr --help
用法:tr [选项]... SET1 [SET2]
Translate, squeeze, and/or delete characters from standard input,
writing to standard output.

  -c, -C, --complement    use the complement of SET1
  -d, --delete            delete characters in SET1, do not translate
  -s, --squeeze-repeats   replace each sequence of a repeated character
                            that is listed in the last specified SET,
                            with a single occurrence of that character
  -t, --truncate-set1     first truncate SET1 to length of 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:]    所有的十六进制数
  [=字符=]      所有和指定字符相等的字符

Translation occurs if -d is not given and both SET1 and SET2 appear.
-t may be used only when translating.  SET2 is extended to length of
SET1 by repeating its last character as necessary.  Excess characters
of SET2 are ignored.  Only [:lower:] and [:upper:] are guaranteed to
expand in ascending order; used in SET2 while translating, they may
only be used in pairs to specify case conversion.  -s uses the last
specified SET, and occurs after translation or deletion.

将文件testfile中的小写字母全部转换成大写字母,此时,可使用如下命令:

cat testfile |tr a-z A-Z 

示例将ab转化为AB

# echo abc |tr ab AB
ABc




Tags:
shell tr

相关主题:

留言信息:

......期待您的留言!


增加留言、提问或者评论,不用注册,匿名提交,你提交的信息经过审核后才会显示:


© 2008-2022 CunYouLu存有录博客 村友录 存游录 鲁ICP备08005943号