用于比较两个大型900 x 900逗号分隔文件的脚本

时间:2015-03-25 11:51:32

标签: linux bash awk

我尝试过awk,但是无法在两个文件上一次为每个单元格执行一次差异。我尝试过awk,但是无法在两个文件上一次为每个单元格执行一次差异。我尝试过awk,但是无法在两个文件上一次为每个单元格执行一次差异。

2 个答案:

答案 0 :(得分:0)

如果你只是想要一个粗略的答案,可能最简单的事情就是做:

tr , \\n file1 > /tmp/output
tr , \\n file2 | diff - /tmp/output

这会将每个文件转换为一列并运行diff。您可以计算与输出的行号不同的单元格。

答案 1 :(得分:0)

使用awk的最简单方法,不考虑字段内的换行符,引用逗号等。

打印相同的

awk 'BEGIN{RS=",|"RS}a[FNR]==$0;{a[NR]=$0}' file{,2}

打印差异

awk 'BEGIN{RS=",|"RS}FNR!=NR&&a[FNR]!=$0;{a[NR]=$0}' file{,2}

打印相同的

awk 'BEGIN{RS=",|"RS}FNR!=NR{print "cell"FNR (a[FNR]==$0?"":" not")" the same"}{a[NR]=$0}' file{,2}

输入

文件

1,2,3,4,5  
6,7,8,9,10  
11,12,13,14,15  

file2的

1,2,3,4,5  
2,7,1,9,12  
1,1,1,1,12  

输出

相同

1
2
3
4
5
7
9

不同的

2
1
12
1
1
1
1
12

相同的

cell1 the same
cell2 the same
cell3 the same
cell4 the same
cell5 the same
cell6 not the same
cell7 the same
cell8 not the same
cell9 the same
cell10 not the same
cell11 not the same
cell12 not the same
cell13 not the same
cell14 not the same
cell15 not the same