1

I have a directory on my Linux system whose contents are file names in pairs as follows:

 File1a
 File1b
 File2a
 File2b
 File3a
 File3b

I want to do a diff between the contents of File1a and File1b and store the results in a separate file. Similarly do this over other pairs iterating through the entire list. Can this be achieved with a shell script ?

4

3 回答 3

2
for x in 1 2 3
do
  diff File${x}a File${x}b > File${x}diff
done

这会将每个差异放入自己的文件中(File1diff例如)。如果您希望将所有差异放入一个文件中,您可以这样做:

for x in 1 2 3
do
  echo "***** Diff of File${x}a <> File${x}b:" >> DiffOutput
  diff File${x}a File${x}b >> DiffOutput
done
于 2013-08-27T13:59:24.623 回答
1

如果您真的需要依赖文件中的内容,因为您的文件名与您发布的简单模式不匹配(并且您的文件名不包含空格),您可以执行以下操作:

xargs -L2 echo < file.txt | while read first second; do
    diff "${first}" "${second}" > "${first}_${second}.diff"
done

否则请使用@devnull 或@mbratch 的解决方案。

于 2013-08-27T14:10:03.603 回答
1

假设文件名的形式为File\d*{a,b}

for i in File*a; do
  diff ${i} ${i%a}b > ${i%a}.diff
done

差异将被重定向到File1.diff一对File1a& File1b

于 2013-08-27T14:04:50.170 回答