NAME
join - join lines of two files on a common field//连接两个文件中相同的字段
SYNOPSIS
join [OPTION]... FILE1 FILE2
DESCRIPTION
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.
-i, --ignore-case
ignore differences in case when comparing fields
-t CHAR
use CHAR as input and output field separator
-1 FIELD
join on this FIELD of file 1
-2 FIELD
join on this FIELD of file 2
================================
e.g.1
[tom@localhost tmp]$ head 1.txt 2.txt
==> 1.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
==> 2.txt <==
100 $1000
200 $1234
300 $892
400 $2389
500 $123
1000 $2389
[tom@localhost tmp]$ join 1.txt 2.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
========================================
e.g.2
[root@localhost tmp]# head -3 /etc/passwd /etc/shadow
==> /etc/passwd <==
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
==> /etc/shadow <==
root:$6$Yn0VmR/GE/f6/9Pj$kfdHuA7JUAwqHlRCuVlYTuGioVeuJthvxzUPdVD.lGJ96.ALqEcXKAVTGW.cU1k8S4P53kYiUVZdCHaWK94tC1:16177:0:99999:7:::
bin:*:15628:0:99999:7:::
daemon:*:15628:0:99999:7:::
[root@localhost tmp]# join -t ':' /etc/passwd /etc/shadow
root:x:0:0:root:/root:/bin/bash:$6$Yn0VmR/GE/f6/9Pj$kfdHuA7JUAwqHlRCuVlYTuGioVeuJthvxzUPdVD.lGJ96.ALqEcXKAVTGW.cU1k8S4P53kYiUVZdCHaWK94tC1:16177:0:99999:7:::
bin:x:1:1:bin:/bin:/sbin/nologin:*:15628:0:99999:7:::
daemon:x:2:2:daemon:/sbin:/sbin/nologin:*:15628:0:99999:7:::
=================================================
e.g.3
[root@localhost tmp]# head -3 /etc/passwd /etc/group
==> /etc/passwd <==
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
==> /etc/group <==
root:x:0:
bin:x:1:bin,daemon
daemon:x:2:bin,daemon
[root@localhost tmp]# join -t':' -1 4 /etc/passwd -2 3 /etc/group
0:root:x:0:root:/root:/bin/bash:root:x:
1:bin:x:1:bin:/bin:/sbin/nologin:bin:x:bin,daemon
2:daemon:x:2:daemon:/sbin:/sbin/nologin:daemon:x:bin,daemon