我有两个文件test1.txt和test2.txt
test1.txt包含
abc.cde.ccd.eed.12345.5678.txt
abcd.cdde.ccdd.eaed.12346.5688.txt
aabc.cade.cacd.eaed.13345.5078.txt
abzc.cdae.ccda.eaed.29345.1678.txt
abac.cdae.cacd.eead.18145.2678.txt
aabc.cdve.cncd.ened.19945.2345.txt和test2.txt包含
12345.5678.txt
29345.1678.txt
18145.2678.txt
10111.2222.txt我想比较这两个文件,并在bash中给出类似的输出
在这两方面:
abc.cde.ccd.eed.12345.5678.txt
abzc.cdae.ccda.eaed.29345.1678.txt
abac.cdae.cacd.eead.18145.2678.txt仅在test1.txt中
abcd.cdde.ccdd.eaed.12346.5688.txt
aabc.cade.cacd.eaed.13345.5078.txt
aabc.cdve.cncd.ened.19945.2345.txt仅在test2.txt中
10111.2222.txt发布于 2014-10-24 17:33:15
在这两方面:
grep -f text2.txt text1.txt输出:
abc.cde.ccd.eed.12345.5678.txt
abzc.cdae.ccda.eaed.29345.1678.txt
abac.cdae.cacd.eead.18145.2678.txt
仅在test1.txt中:
grep -v -f text2.txt text1.txt输出:
abcd.cdde.ccdd.eaed.12346.5688.txt
aabc.cade.cacd.eaed.13345.5078.txt
aabc.cdve.cncd.ened.19945.2345.txt
仅在test2.txt中:
grep -v -f <( grep -Eo '[0-9]+.[0-9]+.txt' text1.txt) text2.txt输出:
10111.2222.txt发布于 2014-10-24 17:28:15
File1 :
abc.cde.ccd.eed.12345.5678.txt
abcd.cdde.ccdd.eaed.12346.5688.txt
aabc.cade.cacd.eaed.13345.5078.txt
abzc.cdae.ccda.eaed.29345.1678.txt
abac.cdae.cacd.eead.18145.2678.txt
aabc.cdve.cncd.ened.19945.2345.txt
File2 :
12345.5678.txt
29345.1678.txt
18145.2678.txt
10111.2222.txt
#!/bin/bash
if [ -e Both.txt ]
then
rm Both.txt
fi
if [ -e File1.txt ]
then
rm File1.txt
fi
if [ -e File2.txt ]
then
rm File2.txt
fi
while read f2line
do
found=0
while read f1line
do
Both=`echo "$f1line" | grep "$f2line"`
if [ $? -eq 0 ]
then
found=1
echo $Both >> Both.txt
fi
done < File1
if [ $found -eq 0 ]
then
echo $f2line >> File2.txt
fi
done < File2
sort Both.txt > s_Both.txt
sort File1 > s_File1
comm -3 s_File1 s_Both.txt > File1.txt
rm s_File1
rm s_Both.txt输出文件: Both.txt,File1.txt,File2.txt
发布于 2014-10-24 18:44:33
下面的AWK脚本script.awk也完成了这项工作:
NR == FNR { lines[++i] = $0 }
NR > FNR { patterns[++j] = $0 }
END {
for (p_index in patterns)
for (l_index in lines)
if (index(lines[l_index], patterns[p_index]) > 0) {
lines_match[l_index] = 1
patterns_match[p_index] = 1
}
print "Lines only in first file:"
for (l_index in lines)
if (!(l_index in lines_match))
print lines[l_index]
print "Lines only in second file:"
for (p_index in patterns)
if (! (p_index in patterns_match))
print patterns[p_index]
print "Lines in both files:"
for (l_index in lines)
if (l_index in lines_match)
print lines[l_index]
}它的名称如下:
awk -f script.awk test1.txt test2.txt请注意,脚本没有对两个文件中的数据结构作出任何假设。它只是假定test2.txt中的行是test1.txt中行的潜在子字符串。
https://stackoverflow.com/questions/26551962
复制相似问题