我有两个文件a.unl和b.unl。内容是:Unix - 如何打印两个文本文件之间的区别?
#a.unl
300111
302130
300042
300054
400045
#b.unl
200032
300042
300111
565000
310056
然后有c.unl来捕捉差异。 如果a.unl中的一行与b.unl中的一行匹配,则它不会打印到c.unl。只有当它不同时才打印。 我怎么能做到这一点?
非常感谢您的帮助。
我有两个文件a.unl和b.unl。内容是:Unix - 如何打印两个文本文件之间的区别?
#a.unl
300111
302130
300042
300054
400045
#b.unl
200032
300042
300111
565000
310056
然后有c.unl来捕捉差异。 如果a.unl中的一行与b.unl中的一行匹配,则它不会打印到c.unl。只有当它不同时才打印。 我怎么能做到这一点?
非常感谢您的帮助。
最好的办法是对这两个文件进行排序,然后在结果上运行comm
。如果你有bash
为你的shell,你可以使用Process Substitution:
comm -3 <(sort a.unl) <(sort b.unl)
这将打印在a.unl
所有的行,但不是在b.unl
,在b.unl
所有的行,但不是在a.unl
(他们将通过一个标签缩进); -3
会抑制a.unl
和b.unl
中的行。
如果没有bash
做,你可能需要类似:
sort a.unl > a.srt
sort b.unl > b.srt
comm -3 a.srt b.srt
rm -f a.srt b.srt
为了让更多的近防弹(所以如果你打断东西它不围绕离开中间文件),那么你需要:
tmp=tmp.$$
trap "rm -f $tmp.?; exit 1" 0 1 2 3 13 15
sort a.unl > $tmp.a
sort b.unl > $tmp.b
comm -3 $tmp.a $tmp.b
rm -f $tmp.?
trap 0
谢谢,这真的有用。顺便说一句,我使用通讯-13和“排序a.unl -o a.unl”,所以我没有不必要的文件躺在。 – junda
使用diff
来显示两个文件之间的差异。
diff -u a.unl b.unl
输出的样子:
--- a.unl 2013-11-15 13:51:00.936845493 +0800
+++ b.unl 2013-11-15 13:51:21.373908098 +0800
@@ -1,5 +1,5 @@
+200032
+400042
300111
-302130
-300042
-300054
-400045
+565000
+310056
-
手段从a.unl删除,+
手段添加到a.unl。
使用man diff
获取更多详细信息。
你可以使用'diff',也许? – 2013-11-15 04:08:50