行と行が同じでないパイプで区切られた複数のファイルを、最初の列に基づいて1つのファイルにマージします。

行と行が同じでないパイプで区切られた複数のファイルを、最初の列に基づいて1つのファイルにマージします。

異なる行/行を持つ複数のパイプで区切られたファイルを、最初の列に基づいて1つのファイルにリンクします。

前任者:

テスト1.txt

1|1
2|2

test2.txt

1|4
2|5
3|6

テスト3.txt

1|7
2|8
3|9
4|10

出力:

1|1|4|7
2|2|5|8
3||6|9
4|||10

例 2: test1.txt

1|1|2
2|3|4

test2.txt

1|4
2|5
3|6

テスト3.txt

1|7
2|8
3|9
4|10

出力:

1|1|2|4|7
2|3|4|5|8
3||||6|9
4|||||10

ベストアンサー1

各ファイルには2つの列と3つのファイルがある表示されたケースにのみ適用されます。

$ join -t '|' -o0,1.2,2.2 -a 1 -a 2 test[12].txt | join -t '|' -o0,1.2,1.3,2.2 -a 1 -a 2 - test3.txt
1|1|4|7
2|2|5|8
3||6|9
4|||10

つまり、最初の2つのファイルに対してリレーショナル完全外部結合を実行し、同じ方法でその出力を3番目のファイルと結合します。これが-a 1 -a 2完全な外部接続になる理由です。 GNUを使用すると、joinオプション-oとそのオプション引数を-o auto

これはスクリプトにまとめることができます。

#!/bin/sh

# sanity check
if [ "$#" -lt 2 ]; then
    echo 'require at least two files' >&2
    exit 1
fi

# temporary files
result=$(mktemp)  # the result of a join
tmpfile=$(mktemp) # temporary file holding a previous result

# remove temporary files on exit
trap 'rm -f "$result" "$tmpfile"' EXIT

# join the first two files
join -t '|' -o auto -a 1 -a 2 "$1" "$2" >"$result"
shift 2

# loop over the remaining files, adding to the result with each
for pathname do
    mv "$result" "$tmpfile"
    join -t '|' -o auto -a 1 -a 2 "$tmpfile" "$pathname" >"$result"
done

# done, output result
cat "$result"

スクリプトはGNUのjoinオプションに依存し、各ファイルの最初の区切りフィールドで-o auto接続が発生し、|ファイルがこのフィールドのアルファベット順にソートされていると仮定します。

最初の2つのファイルをリンクし、残りのファイルごとに1回の対応する接続​​結果を追加します。

質問の最初の例:

$ ./script.sh test[123].txt
1|1|4|7
2|2|5|8
3||6|9
4|||10

質問の2番目の例(質問には間違った数の空のフィールドが表示されます):

$ ./script.sh test[123].txt
1|1|2|4|7
2|3|4|5|8
3|||6|9
4||||10

ファイルがソートされていない場合は、いつでもソートできます(注:bashプロセスを置き換えるためにここに切り替えます)。

#!/bin/bash

# sanity check
if [ "$#" -lt 2 ]; then
    echo 'require at least two files' >&2
    exit 1
fi

# temporary files
result=$(mktemp)  # the result of a join
tmpfile=$(mktemp) # temporary file holding a previous result

# remove temporary files on exit
trap 'rm -f "$result" "$tmpfile"' EXIT

# join the first two files
join -t '|' -o auto -a 1 -a 2 \
    <( sort -t '|' -k1,1 "$1" ) \
    <( sort -t '|' -k1,1 "$2" ) >"$result"
shift 2

# loop over the remaining files, adding to the result with each
for pathname do
    mv "$result" "$tmpfile"

    # note: $tmpfile" would already be sorted

    join -t '|' -o auto -a 1 -a 2 \
        "$tmpfile" \
        <( sort -t '|' -k1,1 "$pathname" ) >"$result"
done

# done, output result
cat "$result"

ユーザーが別のフィールドに参加できるようにするには(使用-f)、異なる区切り文字を使用し-d(使用)、異なる結合タイプを使用します(使用-j)。

#!/bin/bash

# default values
delim='|'
field='1'

join_type=( -a 1 -a 2 ) # full outer join by default

# override the above defaults with options given to us by the user
# on the command line
while getopts 'd:f:j:' opt; do
    case "$opt" in
        d) delim="$OPTARG" ;;
        f) field="$OPTARG" ;;
        j)
            case "$OPTARG" in
                inner) join_type=( ) ;;
                left)  join_type=( -a 1 ) ;;
                right) join_type=( -a 2 ) ;;
                full)  join_type=( -a 1 -a 2 ) ;;
                *) printf 'unknown join type "%s", expected inner, left, right or full\n' "$OPTARG" >&2
                   exit 1
            esac ;;
        *) echo 'error in command line parsing' >&2
           exit 1
    esac
done

shift "$(( OPTIND - 1 ))"

# sanity check
if [ "$#" -lt 2 ]; then
    echo 'require at least two files' >&2
    exit 1
fi

# temporary files
result=$(mktemp)  # the result of a join
tmpfile=$(mktemp) # temporary file holding a previous result

# remove temporary files on exit
trap 'rm -f "$result" "$tmpfile"' EXIT

# join the first two files
join -t "$delim" -j "$field" -o auto "${join_type[@]}" \
    <( sort -t "$delim" -k"$field,$field" "$1" ) \
    <( sort -t "$delim" -k"$field,$field" "$2" ) >"$result"
shift 2

# loop over the remaining files, adding to the result with each
for pathname do
    mv "$result" "$tmpfile"

    # note: $tmpfile would already be sorted and
    #       the join field is the first field in that file

    join -t "$delim" -2 "$field" -o auto "${join_type[@]}" \
        "$tmpfile" \
        <( sort -t "$delim" -k "$field,$field" "$pathname" ) >"$result"
done

# done, output result
cat "$result"

2番目の例をもう一度実行してテストします。

$ ./script.sh test[123].txt
1|1|2|4|7
2|3|4|5|8
3|||6|9
4||||10

同じファイルで実行しますが、2番目のフィールドで結合します。

$ ./script.sh -f 2 test[123].txt
1|1|2||
10||||4
3|2|4||
4|||1|
5|||2|
6|||3|
7||||1
8||||2
9||||3

内部結合を作成します。

$ ./script.sh -j inner test[123].txt
1|1|2|4|7
2|3|4|5|8

おすすめ記事