如何比较同一目录下对应的多个文件

如何比较同一目录下对应的多个文件

您好,我有一个类似的文件夹/mell/908,其中包含如下文件:

cf-mell-pos-908-tcg-4619e.txt
cf-mell-pos-908-tcw-4619e.txt
cf-mell-pos-908-usc-4619e.txt
cf-mell-pos-908-wi_board-4619e.txt
copper_qnt
tcg_mell_upload_lx.txt
tcw_mell_upload_lx.txt
usc_mell_upload_lx.txt
wi_board_mell_upload_lx.txt

有没有办法可以使用命令diff来比较对应的文件

  • cf-mell-pos-908-tcg-4619e.txttcg_mell_upload_lx.txt
  • cf-mell-pos-908-tcw-4619e.txttcw_mell_upload_lx.txt

等等,而无需diff手动将每一对一对一地进行?我正在运行Linux系统。

答案1

我建议这个选项,假设每对文件中匹配的字符串始终位于相同的位置:

# loop over the files that start with 'cf-'
for f in cf-*.txt; do
  # extract the unique "code", e.g 'tcg'
  code=$(echo "$f" | cut -d'-' -f5)

  # match the looped file with the one that starts with the "code"
  echo "diff" *"-${code}"* "${code}"*.txt

  # perform your commands, in this case I use an `echo` to show
  # how the command `diff` will be executed
done

diff cf-mell-pos-908-tcg-4619e.txt tcg_mell_upload_lx.txt
diff cf-mell-pos-908-tcw-4619e.txt tcw_mell_upload_lx.txt
diff cf-mell-pos-908-usc-4619e.txt usc_mell_upload_lx.txt
diff cf-mell-pos-908-wi_board-4619e.txt wi_board_mell_upload_lx.txt

相关内容