内容我有我需要比较他们,因为缺少一个2项,其他拥有的内容的两个文本文件,但我不知道它,因为它们是长。我试过diff
和vimdiff
,但没有运气。我的文件都是此格式的混乱秩序:比较的文本文件,忽略顺序和格式
item1 item2 item3
item8 item10 item6
item32 item12 item7
我怎样才能挑选出的文本文件中的一个已但是却忽略了格式和顺序对方缺乏哪些项目?
内容我有我需要比较他们,因为缺少一个2项,其他拥有的内容的两个文本文件,但我不知道它,因为它们是长。我试过diff
和vimdiff
,但没有运气。我的文件都是此格式的混乱秩序:比较的文本文件,忽略顺序和格式
item1 item2 item3
item8 item10 item6
item32 item12 item7
我怎样才能挑选出的文本文件中的一个已但是却忽略了格式和顺序对方缺乏哪些项目?
Cyrus的例子是迄今为止短,更重要的一点,但认为我会练习一些(详细)awk
ING ...
的样本数据:
$ cat file1
item2 item3
item8 item10 item6
item32 item12 item7
$ cat file2
item1 item2 item3
item8 item6
item12 item7
假设:
一种可能awk
溶液:
$ cat text.awk
BEGIN { RS="" }
NR==FNR { afile=FILENAME ; for (i=1;i<=NF;i++) a[$i]=1 ; next }
{ bfile=FILENAME ; for (i=1;i<=NF;i++) b[$i]=1 }
END {
for (x in a)
{ if (! b[x])
{ printf "missing from %s : %s\n",bfile,x }
}
for (x in b)
{ if (! a[x])
{ printf "missing from %s : %s\n",afile,x }
}
}
RS=""
:集行分离器(RS
)为空字符串;这样将一个文件分割成一个长记录NR==NFR
:如果这是第(二)文件...afile=FILENAME
:保存文件名以后打印for/a[$i]=1
:使用输入字段1-NF作为指标关联数组a
,数组值设置为1(又名“真”)next
:读下一记录,在这种情况下,意味着读取下一个文件NR!=FNR
:如果这是第二(两个)文件...bfile
和关联数组b
END ...
:处理我们的阵列...for (x in a)
:通过数组a
的指标循环,并分配给变量x
,如果有数组b
(! b[x]
)没有类似的索引条目,然后打印有关数组索引消息(实际上的名字从原来的文件项)从失踪bfile
for (x in b)
:同以前的循环检查,除了在bfile
项目,但不是在afile
这awk
在动作脚本:
$ awk -f text.awk file1 file2
missing from file2 : item10
missing from file2 : item32
missing from file1 : item1
# switch the order of the input files => same messages, just different order
$ awk -f text.awk file2 file1
missing from file1 : item1
missing from file2 : item10
missing from file2 : item32
我相信你可以使用通讯命令..但你应该在有序这两个文件进行比较:
comm -23 f1 f2 # will give whatever lines not matching in file1 against file2
comm -12 f1 f2 # will give matching lines
comm -13 f1 f2 # will give whatever lines not matching in file2 against file 1
感谢Mr.batMan。 :)我已纠正它。 – VIRA
使用comm
到您的文件进行比较,以找出最常见的还是在他们不同。
$ cat file1
item1 item2 item3
item8 item10 item6
item32 item12 item5
$ cat file2
item1 item2 item3
item8 item15 item6
item32 item12 item7
comm -23 file1 file2
返回线,是file1中而不是在文件2条
comm -13 file1 file2
返回线,是file2中而不是在文件1条
comm -12 file1 file2
返回线,在这两个文件共同
comm
需要输入文件到排序。我们将首先通过sed将spaces
转换为\n
,然后通过排序进行排序。
$ comm -23 <(sed 's/ \+/\n/g' file1 | sort) <(sed 's/ \+/\n/g' file2 | sort)
item10
item5
$ comm -13 <(sed 's/ \+/\n/g' file1 | sort) <(sed 's/ \+/\n/g' file2 | sort)
item15
item7
$ comm -12 <(sed 's/ \+/\n/g' file1 | sort) <(sed 's/ \+/\n/g' file2 | sort)
item1
item12
item2
item3
item32
item6
item8
- 我的答案在这里结束。 ---
但只为信息,通讯的手册页说:
With no options, comm produce three-column output. Column one contains lines unique to FILE1, column two contains lines unique to FILE2, and column three contains lines common to both files.
-1 suppress column 1 (lines unique to FILE1)
-2 suppress column 2 (lines unique to FILE2)
-3 suppress column 3 (lines that appear in both files)
因此:
$ comm <(sed 's/ \+/\n/g' file1 | sort) <(sed 's/ \+/\n/g' file2 | sort)
item1
item10
item12
item15
item2
item3
item32
item5
item6
item7
item8
你写**两个文本文件**,公布这些输入文件和预期结果 – RomanPerekhrest