0

大きなソート済みファイルを小さなチャンクに分割する必要があります。各ファイルには、ソート済みの個人名のリストが含まれています。たとえば、同じ名前の人が2つのファイルに表示されないことを保証したいと思います。

File1:
.
.
James
James
Kobe

File2:
Kobe
Nash
Nash
.
.

私はそれをする必要があります

File1:
.
.
James
James
Kobe
Kobe

File2:
Nash
Nash
.
.

以前は、sedを使用して手動でこれを行っていました。今、私はこれを自動化するためのbashスクリプトを書きたいのですが、bashに精通していません。

4

1 に答える 1

1

「現在の」ファイルの最後の行と「次の」ファイルの最初の行を比較する必要があります。ファイルの名前は「File1、File2、... FileN」と仮定します。これはテストされていません。

n=1
while true; do
    current=File$n
    next=File$((++n)) 
    if [[ ! -f $next ]]; then
        break
    fi
    last=$(tail -1 "$current")
    first=$(head -1 "$next")
    while [[ $last == $first ]]; do
        echo "$last" >> "$current"    # append the name to the end of the current
        sed -i 1d "$next"             # remove the first line of the next file
        first=$(head -1 "$next")
    done
done

次のファイルから1行を繰り返し削除する可能性があるため、これは少し遅い場合があります。これは少し速いかもしれません: 繰り返しますが、テストされていません。

n=1
while true; do
    current=File$n
    next=File$((++n)) 
    if [[ ! -f $next ]]; then
        break
    fi
    last=$(tail -1 "$current")
    first=$(head -1 "$next")
    num=$(awk -v line="$last" -v N=0 '$0 == line {N++; next} {print N; exit}' "$next")
    if (( num > 0 )); then
        for (( i=1; i<=num; i++ )); do
            echo "$last" >> "$current"
        done
        sed -i "1,$Nd" "$next"
    fi
done
于 2013-02-20T23:06:19.567 に答える