「現在の」ファイルの最後の行と「次の」ファイルの最初の行を比較する必要があります。ファイルの名前は「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