join multiple files

前端 未结 8 1988
春和景丽
春和景丽 2020-12-02 21:11

I am using the standard join command to join two sorted files based on column1. The command is simple join file1 file2 > output_file.

But how do I join 3 or more fil

相关标签:
8条回答
  • 2020-12-02 22:06

    man join:

    NAME
           join - join lines of two files on a common field
    
    SYNOPSIS
           join [OPTION]... FILE1 FILE2
    

    it only works with two files.

    if you need to join three, maybe you can first join the first two, then join the third.

    try:

    join file1 file2 | join - file3 > output
    

    that should join the three files without creating an intermediate temp file. - tells the join command to read the first input stream from stdin

    0 讨论(0)
  • 2020-12-02 22:06

    I created a function for this. First argument is the output file, rest arguments are the files to be joined.

    function multijoin() {
        out=$1
        shift 1
        cat $1 | awk '{print $1}' > $out
        for f in $*; do join $out $f > tmp; mv tmp $out; done
    }
    

    Usage:

    multijoin output_file file*
    
    0 讨论(0)
提交回复
热议问题