千千梦丶琪 发表于 2024-8-23 04:48:45

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

https://i-blog.csdnimg.cn/direct/0f75807ab6c04198a7a485a95fde99bd.png

0 前言


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

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

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

tr --help
Usage: tr ... SET1
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 input sequence of a repeated character
                            that is listed in SET1 with a single occurrence
                            of that character
-t, --truncate-set1   first truncate SET1 to length of SET2
      --help   display this help and exit
      --versionoutput version information and exit

SETs are specified as strings of characters.Most represent themselves.
Interpreted sequences are:

\NNN            character with octal value NNN (1 to 3 octal digits)
\\            backslash
\a            audible BEL
\b            backspace
\f            form feed
\n            new line
\r            return
\t            horizontal tab
\v            vertical tab
CHAR1-CHAR2   all characters from CHAR1 to CHAR2 in ascending order
         in SET2, copies of CHAR until length of SET1
   REPEAT copies of CHAR, REPEAT octal if starting with 0
[:alnum:]       all letters and digits
[:alpha:]       all letters
[:blank:]       all horizontal whitespace
[:cntrl:]       all control characters
[:digit:]       all digits
[:graph:]       all printable characters, not including space
[:lower:]       all lower case letters
[:print:]       all printable characters, including space
[:punct:]       all punctuation characters
[:space:]       all horizontal or vertical whitespace
[:upper:]       all upper case letters
[:xdigit:]      all hexadecimal digits
[=CHAR=]      all characters which are equivalent to CHAR

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 SET1 if not
translating nor deleting; else squeezing uses SET2 and occurs after
translation or deletion.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Report tr translation bugs to <http://translationproject.org/team/>
For complete documentation, run: info coreutils 'tr invocation'
https://i-blog.csdnimg.cn/direct/44b6e108c3534f62894ffc850960a3c0.png
1.1.2 银河麒麟(kylin)体系中的tr命令帮助信息

tr --help
用法:tr [选项]... SET1
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.

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
请向<http://translationproject.org/team/zh_CN.html> 报告tr 的翻译错误
Full documentation at: <http://www.gnu.org/software/coreutils/tr>
or available locally via: info '(coreutils) tr invocation'
https://i-blog.csdnimg.cn/direct/a631dc9e936546e7953a2cc6fb7a2684.jpeg
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。
echo -e "Windows95 1995 June\nWindows98 1998 August\nDOS 1981 May" > t.txt
cat t.txt
Windows95 1995 June
Windows98 1998 August
DOS 1981 May
https://i-blog.csdnimg.cn/direct/81b66b0f75054605b7bd2a24ee3c8206.png
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
cat t.txt
Windows95 1995 June
Windows98 1998 August
DOS 1981 May
cat t.txt | tr a-z A-Z
WINDOWS95 1995 JUNE
WINDOWS98 1998 AUGUST
DOS 1981 MAY
tr a-z A-Z < t.txt
WINDOWS95 1995 JUNE
WINDOWS98 1998 AUGUST
DOS 1981 MAY
https://i-blog.csdnimg.cn/direct/7e941c60ac594c46ad27ca520c383b6d.png
 2.1.2 利用[:lower:]和 [:upper:]

我们要把文件t.txt中的英文大写字母转换为小写字母再表现出来,这里只演示 输入重定向 的方法,即:
   tr [:upper:] [:lower:] < t.txt
cat t.txt
Windows95 1995 June
Windows98 1998 August
DOS 1981 May
tr [:upper:] [:lower:] < t.txt
windows95 1995 june
windows98 1998 august
dos 1981 may
https://i-blog.csdnimg.cn/direct/78c60d2f311e4eaba83279283d7190ce.png
2.2 去除文件中的重复字符

我们要去除文件t.txt内容中的重复数字9再表现,这里只演示 输入重定向 的方法,即:
   tr -s "9" < t.txt 
cat t.txt
Windows95 1995 June
Windows98 1998 August
DOS 1981 May
tr -s "9" < t.txt
Windows95 195 June
Windows98 198 August
DOS 1981 May
https://i-blog.csdnimg.cn/direct/778bb135df174ef69c80e8021f50a0b7.png
可以看到 第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
cat t.txt
Windows95 1995 June
Windows98 1998 August
DOS 1981 May
tr"0-9" "C-L" < t.txt> s.txt
cat s.txt
WindowsLH DLLH June
WindowsLK DLLK August
DOS DLKD May
tr"C-L" "0-9" < s.txt > r.txt
cat r.txt
Windows95 1995 7une
Windows98 1998 August
1OS 1981 May
https://i-blog.csdnimg.cn/direct/fc71297c5f1a41baaa4d8b103ed1b8fa.png
可以看到,将文件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一样的。
cat t.txt
Windows95 1995 June
Windows98 1998 August
DOS 1981 May
tr 'a-zA-Z' 'n-za-mN-ZA-M' < t.txt > s.txt
cat s.txt
Jvaqbjf95 1995 Whar
Jvaqbjf98 1998 Nhthfg
QBF 1981 Znl
tr 'a-zA-Z' 'n-za-mN-ZA-M' < s.txt > r.txt
cat r.txt
Windows95 1995 June
Windows98 1998 August
DOS 1981 May
https://i-blog.csdnimg.cn/direct/2c6792350677477483f2f30e50644661.png
我们可以进一步利用md5sum命令来看看文件r.txt 和文件t.txt的内容是不是一样的。 

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

 将文件t.txt中的空格转换为tab键(\t)。
cat t.txt
Windows95 1995 June
Windows98 1998 August
DOS 1981 May
tr ' ' '\t' < t.txt
Windows95       1995    June
Windows98       1998    August
DOS   1981    May
https://i-blog.csdnimg.cn/direct/3140b8264d0e4fdf8a76a1df56c4f1ed.png
可以看到,文件t.txt中的空格转换为tab键(\t)后,各字段之间表现的间隔更宽了。
2.5 删除文件中的空行

 我们先利用命令
   echo -e "Windows95 1995 June\nWindows98 1998 August\nDOS 1981 May" > t.txt
给t.txt 增长一些空行。
echo -e "\n\nWindows2000\n\n\nwindows XP" >> t.txt
cat t.txt
Windows95 1995 June
Windows98 1998 August
DOS 1981 May


Windows2000


windows XP
https://i-blog.csdnimg.cn/direct/9b5d4a21a58b48d1a696bd9585554473.png
然后我们利用命令
   tr -s "\012" < t.txt

   tr -s "\n" < t.txt
来删除文件中的空行。详细如下:
cat t.txt
Windows95 1995 June
Windows98 1998 August
DOS 1981 May


Windows2000


windows XP
tr -s "\012" < t.txt
Windows95 1995 June
Windows98 1998 August
DOS 1981 May
Windows2000
windows XP
https://i-blog.csdnimg.cn/direct/6daf2cf83d3b4ea799b1961cf964d5eb.png

cat t.txt
Windows95 1995 June
Windows98 1998 August
DOS 1981 May


Windows2000


windows XP
tr -s "\n" < t.txt
Windows95 1995 June
Windows98 1998 August
DOS 1981 May
Windows2000
windows XP
https://i-blog.csdnimg.cn/direct/46a933321f014c4e838c5a1416a0213d.png
以上两条命令都可删除文件中的空行。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: Linux shell编程学习条记72:tr命令——集合转换工具