Wednesday, December 26, 2007
Command in discussion - JOIN - BASH Beginner
"join" is used for joining two input lines having an identical join field. Here is a good example to show its usefulness.
$ cat file1
life is beautiful:1997:Yes
Employee of the Month:2006:Yes
Premonition:2007:No
Happy Feet:2006:Yes
$ cat file2
life is beautiful:8.4
Employee of the Month:5.3
Premonition:5.6
Happy Feet:6.8
Required Output:
life is beautiful:1997:8.4:Yes
Employee of the Month:2006:5.3:Yes
Premonition:2007:5.6:No
Happy Feet:2006:6.8:Yes
This can be achieved using "join"
$ join -t ":" -j 1 -o 1.1 1.2 2.2 1.3 file1 file2
A live example would be getting username and its default login shell listed in /etc/passwd and group name from /etc/group.
Numeric value of "group" is 4th field in /etc/passwd and 3rd field in /etc/group file.
$ join -t ":" -1 4 -2 3 -o 1.1 2.1 1.7 /etc/passwd /etc/group
root:root:/bin/bash
daemon:daemon:/bin/sh
bin:bin:/bin/sh
lp:lp:/bin/sh
mail:mail:/bin/sh
news:news:/bin/sh
proxy:proxy:/bin/sh
...
Subscribe to:
Post Comments (Atom)
© Jadu Saikia www.UNIXCL.com
2 comments:
Nice one. Simple and useful. Could you please let me know what -j flag does in first example? And what is "-1 4 -2 3" in the second one? Thanks.
-j 1
is same as first field (eqv to ::: -1 1)
so
-1 4 -2 3
means first join field is 4 and second join field is 3
Post a Comment