Linux shell编程学习条记72:tr命令——集合转换工具

打印 上一主题 下一主题

主题 1740|帖子 1740|积分 5220



0 前言



在大数据时代,我们要面临大量数据,有时需要对数据举行整理和转换。
在Linux中,我们可以利用 tr命令来整理和转换数据,也可以举行简朴的加解密。
1 tr命令 的帮助信息,功能,格式,选项和参数分析

 我们可以利用命令cut--help来获取帮助信息。
1.1  tr命令 的帮助信息

1.1.1 csdn步伐员研究院linux 中的tr命令帮助信息

  1. [purpleendurer @ bash ~] tr --help
  2. Usage: tr [OPTION]... SET1 [SET2]
  3. Translate, squeeze, and/or delete characters from standard input,
  4. writing to standard output.
  5.   -c, -C, --complement    use the complement of SET1
  6.   -d, --delete            delete characters in SET1, do not translate
  7.   -s, --squeeze-repeats   replace each input sequence of a repeated character
  8.                             that is listed in SET1 with a single occurrence
  9.                             of that character
  10.   -t, --truncate-set1     first truncate SET1 to length of SET2
  11.       --help     display this help and exit
  12.       --version  output version information and exit
  13. SETs are specified as strings of characters.  Most represent themselves.
  14. Interpreted sequences are:
  15.   \NNN            character with octal value NNN (1 to 3 octal digits)
  16.   \\              backslash
  17.   \a              audible BEL
  18.   \b              backspace
  19.   \f              form feed
  20.   \n              new line
  21.   \r              return
  22.   \t              horizontal tab
  23.   \v              vertical tab
  24.   CHAR1-CHAR2     all characters from CHAR1 to CHAR2 in ascending order
  25.   [CHAR*]         in SET2, copies of CHAR until length of SET1
  26.   [CHAR*REPEAT]   REPEAT copies of CHAR, REPEAT octal if starting with 0
  27.   [:alnum:]       all letters and digits
  28.   [:alpha:]       all letters
  29.   [:blank:]       all horizontal whitespace
  30.   [:cntrl:]       all control characters
  31.   [:digit:]       all digits
  32.   [:graph:]       all printable characters, not including space
  33.   [:lower:]       all lower case letters
  34.   [:print:]       all printable characters, including space
  35.   [:punct:]       all punctuation characters
  36.   [:space:]       all horizontal or vertical whitespace
  37.   [:upper:]       all upper case letters
  38.   [:xdigit:]      all hexadecimal digits
  39.   [=CHAR=]        all characters which are equivalent to CHAR
  40. Translation occurs if -d is not given and both SET1 and SET2 appear.
  41. -t may be used only when translating.  SET2 is extended to length of
  42. SET1 by repeating its last character as necessary.  Excess characters
  43. of SET2 are ignored.  Only [:lower:] and [:upper:] are guaranteed to
  44. expand in ascending order; used in SET2 while translating, they may
  45. only be used in pairs to specify case conversion.  -s uses SET1 if not
  46. translating nor deleting; else squeezing uses SET2 and occurs after
  47. translation or deletion.
  48. GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
  49. Report tr translation bugs to <http://translationproject.org/team/>
  50. For complete documentation, run: info coreutils 'tr invocation'
  51. [purpleendurer @ bash ~]
复制代码

1.1.2 银河麒麟(kylin)体系中的tr命令帮助信息

  1. [purpleenduer @ kylin ~ ] tr --help
  2. 用法:tr [选项]... SET1 [SET2]
  3. Translate, squeeze, and/or delete characters from standard input,
  4. writing to standard output.
  5.   -c, -C, --complement    use the complement of SET1
  6.   -d, --delete            delete characters in SET1, do not translate
  7.   -s, --squeeze-repeats   replace each sequence of a repeated character
  8.                             that is listed in the last specified SET,
  9.                             with a single occurrence of that character
  10.   -t, --truncate-set1     first truncate SET1 to length of SET2
  11.       --help                显示此帮助信息并退出
  12.       --version                显示版本信息并退出
  13. SET 是一组字符串,一般都可按照字面含义理解。解析序列如下:
  14.   \NNN        八进制值为NNN 的字符(1 至3 个数位)
  15.   \\                反斜杠
  16.   \a                终端鸣响
  17.   \b                退格
  18.   \f                换页
  19.   \n                换行
  20.   \r                回车
  21.   \t                水平制表符
  22.   \v                垂直制表符
  23.   字符1-字符2        从字符1 到字符2 的升序递增过程中经历的所有字符
  24.   [字符*]        在SET2 中适用,指定字符会被连续复制直到吻合设置1 的长度
  25.   [字符*次数]        对字符执行指定次数的复制,若次数以 0 开头则被视为八进制数
  26.   [:alnum:]        所有的字母和数字
  27.   [:alpha:]        所有的字母
  28.   [:blank:]        所有呈水平排列的空白字符
  29.   [:cntrl:]        所有的控制字符
  30.   [:digit:]        所有的数字
  31.   [:graph:]        所有的可打印字符,不包括空格
  32.   [:lower:]        所有的小写字母
  33.   [:print:]        所有的可打印字符,包括空格
  34.   [:punct:]        所有的标点字符
  35.   [:space:]        所有呈水平或垂直排列的空白字符
  36.   [:upper:]        所有的大写字母
  37.   [:xdigit:]        所有的十六进制数
  38.   [=字符=]        所有和指定字符相等的字符
  39. Translation occurs if -d is not given and both SET1 and SET2 appear.
  40. -t may be used only when translating.  SET2 is extended to length of
  41. SET1 by repeating its last character as necessary.  Excess characters
  42. of SET2 are ignored.  Only [:lower:] and [:upper:] are guaranteed to
  43. expand in ascending order; used in SET2 while translating, they may
  44. only be used in pairs to specify case conversion.  -s uses the last
  45. specified SET, and occurs after translation or deletion.
  46. GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
  47. 请向<http://translationproject.org/team/zh_CN.html> 报告tr 的翻译错误
  48. Full documentation at: <http://www.gnu.org/software/coreutils/tr>
  49. or available locally via: info '(coreutils) tr invocation'
  50. [purpleenduer @ kylin ~ ]
复制代码

1.2 tr命令的功能

tr命令源于英文单词translate,其功能是从尺度输入装备读取数据,举行字符转换、压缩和/或删除后,将效果输出到尺度输出装备,或者重定向到文件。

1.3 tr命令的格式

   tr [选项]... 字符集合1 [字符集合2]
  1.4 tr命令的选项分析

选项分析-c, -C, --complement利用 SET1 的补码。也就是符合 SET1 的部份不做处理,不符合的剩余部份才举行转换-d, --delete删除 SET1 中的字符,不转换 -s, --squeeze-repeats将 SET1 中列出的重复字符缩减为单个字符-t, --truncate-set1减少 SET1 指定范围,使之与 SET2 设定长度相称--help表现此帮助信息并退出--version表现版本信息并退出 1.5 tr命令的字符集合的分析

字符集合指定了字符串范围。
字符串集合1用于查询, 字符集合2用于处理各种转换。
tr刚执行时,字符集合1中的字符被映射到字符集合2中的字符,然后转换操作开始。
表达的序列是:
字符集合分析\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:]所有的十六进制数[=字符=]所有和指定字符相称的字符 一些
速记符寄义八进制方式
\a Ctrl-G铃声\007
\b Ctrl-H退格符\010
\f Ctrl-L走行换页\014
\n Ctrl-J新行\012
\r Ctrl-M回车\015
\t Ctrl-Itab键\011
\v Ctrl-X\030

假如未给出 -d 并且 字符集合1 和 字符集合2  都出现,则会发生转换。
-t 只能在转换时利用。
字符集合2 通过根据需要重复其末了一个字符来扩展到 字符集合1 的长度。
字符集合2 的多余字符将被忽略。
只有 [:lower:] 和 [:upper:] 保证按升序扩展;在翻译时在 字符集合2 中利用,他们大概会
仅成对利用以指定大小写转换。
-s 利用末了指定的 字符集合,并在转换或删除后出现。
2 tr命令的利用实例 

2.0 创建演示文件

为了演示tr命令的用法,我们先创建一个测试文件t.txt。
  1. [purpleendurer @ bash ~] echo -e "Windows95 1995 June\nWindows98 1998 August\nDOS 1981 May" > t.txt
  2. [purpleendurer @ bash ~] cat t.txt
  3. Windows95 1995 June
  4. Windows98 1998 August
  5. DOS 1981 May
  6. [purpleendurer @ bash ~]
复制代码

2.1 文件中的英文大小写字母转换

2.1.1利用a-z和 A-Z

我们要把文件t.txt中的英文小写字母转换为大写字母再表现出来,可以利用两种方式来实现:
   1.管道操作:cat t.txt | tr a-z A-Z
    2.输入重定向:tr a-z A-Z < t.txt
  1. [purpleendurer @ bash ~] cat t.txt
  2. Windows95 1995 June
  3. Windows98 1998 August
  4. DOS 1981 May
  5. [purpleendurer @ bash ~] cat t.txt | tr a-z A-Z
  6. WINDOWS95 1995 JUNE
  7. WINDOWS98 1998 AUGUST
  8. DOS 1981 MAY
  9. [purpleendurer @ bash ~] tr a-z A-Z < t.txt
  10. WINDOWS95 1995 JUNE
  11. WINDOWS98 1998 AUGUST
  12. DOS 1981 MAY
  13. [purpleendurer @ bash ~]
复制代码

 2.1.2 利用[:lower:]和 [:upper:]

我们要把文件t.txt中的英文大写字母转换为小写字母再表现出来,这里只演示 输入重定向 的方法,即:
   tr [:upper:] [:lower:] < t.txt
  1. [purpleendurer @ bash ~] cat t.txt
  2. Windows95 1995 June
  3. Windows98 1998 August
  4. DOS 1981 May
  5. [purpleendurer @ bash ~] tr [:upper:] [:lower:] < t.txt
  6. windows95 1995 june
  7. windows98 1998 august
  8. dos 1981 may
  9. [purpleendurer @ bash ~]
复制代码

2.2 去除文件中的重复字符

我们要去除文件t.txt内容中的重复数字9再表现,这里只演示 输入重定向 的方法,即:
   tr -s "9" < t.txt 
  1. [purpleendurer @ bash ~] cat t.txt
  2. Windows95 1995 June
  3. Windows98 1998 August
  4. DOS 1981 May
  5. [purpleendurer @ bash ~] tr -s "9" < t.txt
  6. Windows95 195 June
  7. Windows98 198 August
  8. DOS 1981 May
  9. [purpleendurer @ bash ~]
复制代码

可以看到 第1行中的1995变成了 195,第2行中的1998 变成了 198。
2.3 将数字转换为字母再从字母转换回数字(加密息争密)

2.3.1 简朴的转换

将文件t.txt中的数字0-9转换为大写英笔墨母F-L,存储到文件s.txt,即: 
    tr  "0-9" "C-L" < t.txt  > s.txt
  再将文件s.txt中的大写英笔墨母F-L转换为数字0-9存储到文件r.txt,即:
   tr  "C-L" "0-9" < s.txt > r.txt
  1. [purpleendurer @ bash ~] cat t.txt
  2. Windows95 1995 June
  3. Windows98 1998 August
  4. DOS 1981 May
  5. [purpleendurer @ bash ~]  tr  "0-9" "C-L" < t.txt  > s.txt
  6. [purpleendurer @ bash ~] cat s.txt
  7. WindowsLH DLLH June
  8. WindowsLK DLLK August
  9. DOS DLKD May
  10. [purpleendurer @ bash ~]  tr  "C-L" "0-9" < s.txt > r.txt
  11. [purpleendurer @ bash ~] cat r.txt
  12. Windows95 1995 7une
  13. Windows98 1998 August
  14. 1OS 1981 May
  15. [purpleendurer @ bash ~]
复制代码

可以看到,将文件t.txt中的数字0-9转换为大写英笔墨母F-L,这个加密过程没有问题。
在将文件s.txt中的大写英笔墨母F-L转换为数字0-9,这个解密过程出现了问题,就是第1行中June的J被转换为7,第3行中DOS的D被转换为1。
以是加解密算法照旧有讲求,需要精心设计的。
2.3.2 ROT13加密算法

ROT13(Rotate by 13 Places)是一个著名的对称加密算法,它的加密算法就是通过将字母表中的每个字母向后移动13个位置来加密文本。
ROT13是一种对称加密算法,这意味着加密息争密过程是相同的,因此解密的方法就是将加密后的文本再次利用ROT13举行加密,这样就会得到原始的文本。
下面我们利用ROT13加密算法对文件t.txt举行加密,储存到文件s.txt,利用ROT13加密算法对文件s.txt举行加密,储存到文件r.txt,那么文件r.txt 的内容应该是和文件t.txt一样的。
  1. [purpleendurer @ bash ~] cat t.txt
  2. Windows95 1995 June
  3. Windows98 1998 August
  4. DOS 1981 May
  5. [purpleendurer @ bash ~] tr 'a-zA-Z' 'n-za-mN-ZA-M' < t.txt > s.txt
  6. [purpleendurer @ bash ~] cat s.txt
  7. Jvaqbjf95 1995 Whar
  8. Jvaqbjf98 1998 Nhthfg
  9. QBF 1981 Znl
  10. [purpleendurer @ bash ~] tr 'a-zA-Z' 'n-za-mN-ZA-M' < s.txt > r.txt
  11. [purpleendurer @ bash ~] cat r.txt
  12. Windows95 1995 June
  13. Windows98 1998 August
  14. DOS 1981 May
  15. [purpleendurer @ bash ~]
复制代码

我们可以进一步利用md5sum命令来看看文件r.txt 和文件t.txt的内容是不是一样的。 

  1. [purpleendurer @ bash ~] md5sum t.txt
  2. 95ffe6a8713a31e34ed3daffe500b628  t.txt
  3. [purpleendurer @ bash ~] md5sum r.txt
  4. 95ffe6a8713a31e34ed3daffe500b628  r.txt
  5. [purpleendurer @ bash ~]
复制代码

文件r.txt 和文件t.txt的MD5值都是95ffe6a8713a31e34ed3daffe500b628,分析二者的内容是一样的。
关于md5sum命令的用法,可以参考:
Linux shell编程学习条记42:md5sum-CSDN博客
https://blog.csdn.net/Purpleendurer/article/details/137125672Linux shell编程学习条记44:编写一个脚本,将md5sum命令执行效果生存到变量中,进而比较两个文件内容是否相同_md5sum -c 值赋给变量-CSDN博客
https://blog.csdn.net/Purpleendurer/article/details/137128034
2.4 将文件中的空格转换为tab键

 将文件t.txt中的空格转换为tab键(\t)。
  1. [purpleendurer @ bash ~] cat t.txt
  2. Windows95 1995 June
  3. Windows98 1998 August
  4. DOS 1981 May
  5. [purpleendurer @ bash ~] tr ' ' '\t' < t.txt
  6. Windows95       1995    June
  7. Windows98       1998    August
  8. DOS     1981    May
  9. [purpleendurer @ bash ~]
复制代码

可以看到,文件t.txt中的空格转换为tab键(\t)后,各字段之间表现的间隔更宽了。
2.5 删除文件中的空行

 我们先利用命令
   echo -e "Windows95 1995 June\nWindows98 1998 August\nDOS 1981 May" > t.txt
  给t.txt 增长一些空行。
  1. [purpleendurer @ bash ~] echo -e "\n\nWindows2000\n\n\nwindows XP" >> t.txt
  2. [purpleendurer @ bash ~] cat t.txt
  3. Windows95 1995 June
  4. Windows98 1998 August
  5. DOS 1981 May
  6. Windows2000
  7. windows XP
  8. [purpleendurer @ bash ~]
复制代码

然后我们利用命令
   tr -s "\012" < t.txt
  或
   tr -s "\n" < t.txt
  来删除文件中的空行。详细如下:
  1. [purpleendurer @ bash ~] cat t.txt
  2. Windows95 1995 June
  3. Windows98 1998 August
  4. DOS 1981 May
  5. Windows2000
  6. windows XP
  7. [purpleendurer @ bash ~] tr -s "\012" < t.txt
  8. Windows95 1995 June
  9. Windows98 1998 August
  10. DOS 1981 May
  11. Windows2000
  12. windows XP
  13. [purpleendurer @ bash ~]
复制代码


  1. [purpleendurer @ bash ~] cat t.txt
  2. Windows95 1995 June
  3. Windows98 1998 August
  4. DOS 1981 May
  5. Windows2000
  6. windows XP
  7. [purpleendurer @ bash ~] tr -s "\n" < t.txt
  8. Windows95 1995 June
  9. Windows98 1998 August
  10. DOS 1981 May
  11. Windows2000
  12. windows XP
  13. [purpleendurer @ bash ~]
复制代码

以上两条命令都可删除文件中的空行。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

千千梦丶琪

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表