曲径通幽论坛

 找回密码
 立即注册
搜索
查看: 4146|回复: 0
打印 上一主题 下一主题

join --- 资料的连结

[复制链接]

4918

主题

5880

帖子

3万

积分

GROAD

曲径通幽,安觅芳踪。

Rank: 6Rank: 6

积分
34387
跳转到指定楼层
楼主
发表于 2009-1-11 11:40:30 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
命令描述1( 来自 man ):
For  each  pair of input lines with identical join fields, write a line to standard output.  The default join field is the first, delimited  by whitespace.   When FILE1 or FILE2 (not both) is -, read standard input.

对于每一对有着相同的连结域的输入行,进行整合整理成一行后输出到标准输出。默认的连结域在第一栏中,这一栏的分隔符是空格。当 FILE1 或 FILE2 (不是两个同时)是 - , 从标准输入读入。

命令描述2( 来自 info ):
`join' writes to standard output a line for each pair of input lines that have identical join fields.  Synopsis:
     join [OPTION]... FILE1 FILE2
Either FILE1 or FILE2 (but not both) can be `-', meaning standard input.  FILE1 and FILE2 should be sorted on the join fields.
'join' 对于有相同连结域的一对输入行进行整合后输出。命令格式是:
               join [OPTION]... FILE1 FILE2

FILE1 或者 FILE2(不是两个同时)可以是 '-' (意味着是标准输入)。FILE1 和 FILE2 在连结域里将被 sort 起来。

对于 FILE2 是 '-' 的举例
[beyes@localhost ~]$ cat temp.txt
root:x:1:kk:22:ee:duoduo:hello:yes:no

[beyes@localhost ~]$ join -t ':' temp.txt -
root:yes:ok                                  #这里进行输入,ctrl + D 结束输入
root:x:1:kk:22:ee:duoduo:hello:yes:no:yes:ok

-a 参数的使用
设 test-1.txt 中的内容为
yes:no:duoduo:i:love:you
beyes::admin::root::qingqing

设 test-2.txt 中的内容为
yes:hehe:haha:hoho:bebe:kiki
3:5:6:7:8:9

执行下面的命令
[beyes@localhost com_join]$ join -t ':' -a 1 test-1.txt test-2.txt
yes:no:duoduo:i:love:you:hehe:haha:hoho:bebe:kiki      
beyes::admin::root::qingqing               #可见出了进行匹配连结外还输出了 test-1.txt 中不匹配的行

基本应用参数
-a  :从 FILENUM 中打印出不匹配的行,FILENUM 为1或2,也就是相应的代表 FILE1 或 FILE2

-t  :join 默认以空白符作为分隔符,并且比较『第一個欄位』的資料,
      如果兩個檔案相同,則將兩筆資料聯成一行,且第一個欄位放在第一個!

-i  :忽略大小写差别

-1  :這個是數字的 1 ,代表『第一個檔案要用那個欄位來分析』的意思;

-2  :代表『第二個檔案要用那個欄位來分析』的意思。



将 /etc/passwd 和 /etc/shadow 进行整合:
[root@localhost beyes]# join -t ':' /etc/passwd /etc/shadow
...
bin:x:1:1:bin:/bin:/sbin/nologin:*:14215:0:99999:7:::
daemon:x:2:2:daemon:/sbin:/sbin/nologin:*:14215:0:99999:7:::
adm:x:3:4:adm:/var/adm:/sbin/nologin:*:14215:0:99999:7:::
... ...
//说明
在 /etc/shadow 中,每一行的内容是以 : 号为分隔符;在每行的第一栏中,也是和 /etc/passwd 每行第一栏中一样的用户名。所以,在两个文件进行整合时,/etc/shadow 中的用户名就以 * 号表示。

假如另有一文件 temp.txt ,其中内容只有一行,那么连结整合后:
[root@localhost beyes]# join -t ':' /etc/passwd ./temp.txt
root:x:0:0:root:/root:/bin/bash:x:1:kk:22:::
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|曲径通幽 ( 琼ICP备11001422号-1|公安备案:46900502000207 )

GMT+8, 2024-6-2 18:24 , Processed in 0.098526 second(s), 23 queries .

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表