一直在努力工作只是不能让它工作,我尝试从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
它完美无缺
答案 0 :(得分:1)
Awk
解决方案:
awk 'NR==FNR{ a[$0]; next }
{
r = $0; m = "";
sub(/ http:.*/, "");
if ($0 in a) delete a[$0]; else m = "----> ";
print m r
}' file1 file2
r = $0
- 分配了当前已处理记录的变量m
- 变量旨在成为marker
输出:
#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
答案 1 :(得分:1)
您能否请关注awk
并告诉我这是否对您有所帮助。
awk -v s1="---->" 'FNR==NR{a[$0]=$0;next} {val=$0;sub(/ http.*/,"",val);printf("%s\n",val in a?$0:s1 OFS $0)}' Input_file1 Input_file2
现在也添加非单线形式的解决方案。
awk -v s1="---->" '
FNR==NR{ a[$0]=$0;next }
{
val=$0;
sub(/ http.*/,"",val);
printf("%s\n",val in a?$0:s1 OFS $0)
}
' Input_file1 Input_file2
答案 2 :(得分:1)
$ cat tst.awk
NR==FNR {
keys[$0]
next
}
{
key = $0
sub(/ [^ ]+$/,"",key)
print (key in keys ? "" : "----> ") $0
}
$ awk -f tst.awk file1 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
答案 3 :(得分:0)
统一差异怎么样? e.g:
diff -u file1 <(awk 'NF--' file2)
输出:
--- file1 2018-03-26 14:59:49.569347677 +0200
+++ /proc/self/fd/11 2018-03-26 15:01:34.117800718 +0200
@@ -1,6 +1,9 @@
#4 and a row (2)
+1 hello post (5)
+Not over yet (3)
10 Years After (6)
+Can get it done (2)
21 & Over (8)
50_50 (1)
+hear me loud (5)
Almost Christmas (3)