您需要将“当前”文件的最后一行与“下一个”文件的第一行进行比较。我假设您的文件名为“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
这可能有点慢,因为您可能会反复从下一个文件中删除一行。这可能会更快一些:再次,未经测试。
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