join multiple files

前端 未结 8 2010
春和景丽
春和景丽 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条回答
  •  萌比男神i
    2020-12-02 21:57

    One can join multiple files (N>=2) by constructing a pipeline of joins recursively:

    #!/bin/sh
    
    # multijoin - join multiple files
    
    join_rec() {
        if [ $# -eq 1 ]; then
            join - "$1"
        else
            f=$1; shift
            join - "$f" | join_rec "$@"
        fi
    }
    
    if [ $# -le 2 ]; then
        join "$@"
    else
        f1=$1; f2=$2; shift 2
        join "$f1" "$f2" | join_rec "$@"
    fi
    

提交回复
热议问题