【发布时间】:2018-09-04 14:57:32
【问题描述】:
一整天都在工作,只是无法正常工作,我从 diff awk sed 中尝试的代码太多,可以再次记住我尝试过的代码,
这是我的问题,我有 2 个文件(file1 和 file2)
File1 :
#4 and a row (2)
+1 hello post (5)
10 Years After (6)
21 & Over (8)
50_50 (1)
Almost Christmas (3)
File2:
#4 and a row (2) http://example.com/post1
+1 hello post (5) http://example.com/post2
Not over yet (3) http://example.com/post12
10 Years After (6) http://example.com/post3
Can get it done (2) http://example.com/post24
21 & Over (8) http://example.com/post9
50_50 (1) http://example.com/post7
hear me loud (5) http://example.com/post258
Almost Christmas (3) http://example.com/post5
我的问题是如何比较这两个文件并生成这样的 File3 输出
#4 and a row (2) http://example.com/post1
+1 hello post (5) http://example.com/post2
----> Not over yet (3) http://example.com/post12
10 Years After (6) http://example.com/post3
----> Can get it done (2) http://example.com/post24
21 & Over (8) http://example.com/post9
50_50 (1 http://example.com/post7
----> hear me loud (5) http://example.com/post258
Almost Christmas (3) http://example.com/post5
----> 表示此文本行不在 file1 中。
我希望我已经解释得足够好,如果可能的话,请帮助我,因为我缺乏 linux 技能,谢谢你!并希望有人能帮我解决这个问题。
~干杯~
@RavinderSingh13 的解决方案
awk -v s1="---->" 'FNR==NR{a[$0]=$0;next} {val=$0;sub(/ http.*/,"",val);printf("%s\n",val in a?$0:s1 OFS $0)}' file1 file2
效果很好
【问题讨论】:
-
您可以使用 winmerge 直观地比较文件,但您不会得到所需的输出