Home » Questions » Computers [ Ask a new question ]

How to join files on the command line without creating temp files?

How to join files on the command line without creating temp files?

I have two files in a Linux / Bash environment:

Asked by: Guest | Views: 318
Total answers/comments: 2
bert [Entry]

"The following will work in the bash shell:

# Join two files
$ join <(sort abc) <(sort bcd)
1 b d
2 a b

You can join on any column as long as you sort the input files on that column

# Join on the second field
$ join -j2 <(sort -k2 abc) <(sort -k2 bcd)
b 1 2
c 3 5

The -k2 argument to sort means sort on the second column. The -j2 argument to join means join on the second columns. Alternatively join -1 x -2 y file1 file2 will join on the xth column of file1 and the yth column of file2."
bert [Entry]

"This will work in bash shell:

# Join two files
$ sort abc | join - <(sort bcd)
1 b d
2 a b

OR

# Join two files
$ sort bcd | join <(sort abc) -
1 b d
2 a b

Because join can read standard input by using '-'."