红联Linux门户
Linux帮助

Linux命令里面两个很经典的命令:join和tr

发布时间:2014-08-14 15:34:58来源:linux网站作者:余超

下面先看一下join命令的使用方法:
对于合并两个具有相同文件头的方法合并起来是一种很好的方法:


[linux @linux source]$cat sinadept.txt
100 Harry Potter:100:Magic School
200 Alex Jason:200:Sales
300 Yu Chao:300:SINA Product Development
400 Sanjay Gupta:400:Support
500 Nisha Singh:500:Sales
1000 Yu Jun:1000:Peking University EECS
[linux @linux source]$cat sinasalary.txt
100 $1000
200 $1234
300 $892
400 $2389
500 $123
1000 $2389
[linux @linux source]$join sinadept.txt sinasalary.txt
100 Harry Potter:100:Magic School $1000
200 Alex Jason:200:Sales $1234
300 Yu Chao:300:SINA Product Development $892
400 Sanjay Gupta:400:Support $2389
500 Nisha Singh:500:Sales $123
1000 Yu Jun:1000:Peking University EECS $2389
[linux @linux source]$join sinasalary.txt sinadept.txt
100 $1000 Harry Potter:100:Magic School
200 $1234 Alex Jason:200:Sales
300 $892  Yu Chao:300:SINA Product Development
400 $2389 Sanjay Gupta:400:Support
500 $123 Nisha Singh:500:Sales
1000 $2389 Yu Jun:1000:Peking University EECS


再看一下tr命令,对于需要执行整篇文章进行大小写转换的处理时很有用:

如下:


[linux @linux source]$tr a-z A-Z <sinadept.txt
100 HARRY POTTER:100:MAGIC SCHOOL
200 ALEX JASON:200:SALES
300 YU CHAO:300:SINA PRODUCT DEVELOPMENT
400 SANJAY GUPTA:400:SUPPORT
500 NISHA SINGH:500:SALES
1000 YU JUN:1000:PEKING UNIVERSITY EECS
[linux @linux source]$tr A-Z a-z <sinadept.txt
100 harry potter:100:magic school
200 alex jason:200:sales
300 yu chao:300:sina product development
400 sanjay gupta:400:support
500 nisha singh:500:sales
1000 yu jun:1000:peking university eecs
[linux @linux source]$


如果文件过大的话处理起来很方便,如果你会VIM或者是其他工具的话,也有很好的方法用来处理同样的问题。