[Linux](十) --shell bash学习----tr,col,join,paste,expand

1.tr可以进行文件内容的删除和替换工作。

先看看函数:
[Linux](十) --shell bash学习----tr,col,join,paste,expand

删除:

例子:删除test1.txt中的冒号。
cat ~/test1.txt | tr -d ':'
[Linux](十) --shell bash学习----tr,col,join,paste,expand

替换:

例子:替换test1.txt中的‘:’为‘#’。
[Linux](十) --shell bash学习----tr,col,join,paste,expand


2.paste:将两个文件的两行贴在一起,默认用tab键分隔

[Linux](十) --shell bash学习----tr,col,join,paste,expand

例子:默认使用tab键分隔的

paste ~/test1.txt ~/test2.txt
[Linux](十) --shell bash学习----tr,col,join,paste,expand

例子:自定义分隔的符号

[Linux](十) --shell bash学习----tr,col,join,paste,expand

因为test1.txt4,5行是空的,test2没有4,5行,所以只显示了一个#。

例子:配合标准输入输出使用,也就是使用‘-’

[Linux](十) --shell bash学习----tr,col,join,paste,expand

这里面的 - 就代表了 ~/test2.txt的内容。


3.join:拼接两个文件的数据,这个我一开始没看懂…

[Linux](十) --shell bash学习----tr,col,join,paste,expand

  • -i:忽略大小写
  • -1 : 代表第一个文件使用哪一个字段
  • -2 : 代表第二个文件 使用哪一个字段
例子:

[Linux](十) --shell bash学习----tr,col,join,paste,expand

相同的部分是user1+test1.txt的部分+test2.txt的部分

例子:稍微改了下test1和test2中的内容,在来实践,如图

[Linux](十) --shell bash学习----tr,col,join,paste,expand

如果你以’:'为分隔符,可以看到

  • test1中user1,user2,user3都在第4个字段
  • test2中user1,user2,user3都在第二个字段
    说明他们是有相同部分的。
然后我们来进行一个拼接。

join -t ':' -1 4 ~/test1.txt -2 2 ~/test2.txt
[Linux](十) --shell bash学习----tr,col,join,paste,expand

  • 红色是相同的部分。
  • 黄色是test1的剩下部分。
  • 蓝色是test2的剩下部分。

4.expand:将[tab]按键转换成一定数量的空格键。

函数说明:
[Linux](十) --shell bash学习----tr,col,join,paste,expand

例子:可以看到test1.txt文件中有两个tab键,^I代表一个[tab]键。

[Linux](十) --shell bash学习----tr,col,join,paste,expand

用expand进行转换:

expand -t 1 ~/test1.txt
[Linux](十) --shell bash学习----tr,col,join,paste,expand

每一个^I都转换成个一个空格。

5.col也可以把tab键转换成对等的空格键,一般用来进行简单的处理

col [-xb]:
-x:将tab键转换成对等的空格键
-b:在文字内有反斜杠时,只保留最后接的那个字符。
例子:

[Linux](十) --shell bash学习----tr,col,join,paste,expand

可以看到,使用了col-x,tab键转换成了对等的空格。