您需要比較「當前」文件的最後一行和「下一個」文件的第一行。我假設你的文件被命名爲「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
你用什麼命令手動完成它?將這些命令放在腳本中。 – 2013-02-20 21:53:32