Sak*_*Sak 9 shell diff text-processing
我有两个包含以下数据的文件;我需要两个文件之间的区别。
我尝试过,diff但它也显示了两个文件中常见的行:(22372 Dec 4 15:36 /opt/apache-tomcat-6.0.36/webapps/new/new.txt).
第一个文件:(文件1中以相同方式存在多个数据)
22677 Dec 4 15:36 /opt/apache-tomcat-6.0.36/webapps/new/abc.txt
22372 Dec 4 15:36 /opt/apache-tomcat-6.0.36/webapps/new/new.txtRun Code Online (Sandbox Code Playgroud)
第二个文件:(文件 2 中以相同方式存在多个数据)。
22372 Dec 4 15:36 /opt/apache-tomcat-6.0.36/webapps/new/new.txt
22677 Dec 3 15:36 /opt/apache-tomcat-6.0.36/webapps/new/abc.txt
12344 Dec 10 15:36 /opt/apache-tomcat-6.0.36/webapps/abc/.../test.txtRun Code Online (Sandbox Code Playgroud)
我需要以下输出:
22677 Dec 3 15:36 /opt/apache-tomcat-6.0.36/webapps/new/abc.txt
12344 Dec 10 15:36 /opt/apache-tomcat-6.0.36/webapps/abc/.../test.txtRun Code Online (Sandbox Code Playgroud)
Jam*_*ese 19
这似乎是使用 comm 的绝佳机会。
来自 GNU coreutils 手册页 (v8.30):
With no options, 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)
Run Code Online (Sandbox Code Playgroud)
使用此信息,我们可以删除唯一的行file1以及两个文件中存在的行。
$ comm -1 -3 <(sort file1) <(sort file2)
12344 Dec 10 15:36 /opt/apache-tomcat-6.0.36/webapps/abc/.../test.txt
22677 Dec 3 15:36 /opt/apache-tomcat-6.0.36/webapps/new/abc.txt
Run Code Online (Sandbox Code Playgroud)
-1 和 -3 删除文件 1 独有的所有行以及两者共有的所有行。
由于排序,它会改变输出的顺序,但这似乎不是基于问题的考虑因素。
如果输入已经排序,则可以跳过排序产生
$ comm -1 -3 file1 file2
Run Code Online (Sandbox Code Playgroud)
使用diff -u file1 file2 | sed -nr 's/^+([^+].*)/\1/p'
输出:
22677 12 月 3 日 15:36 /opt/apache-tomcat-6.0.36/webapps/new/abc.txt
12344 12 月 10 日 15:36 /opt/apache-tomcat-6.0.36/webapps/abc/.../test 。TXT
如果您需要它们之间的空行,请使用
diff -u file1 file2 | sed -nr 's/^+([^+].*)/\1\n/p'
输出:
22677 12 月 3 日 15:36 /opt/apache-tomcat-6.0.36/webapps/new/abc.txt
12344 12 月 10 日 15:36 /opt/apache-tomcat-6.0.36/webapps/abc/.../test.txt
| 归档时间: |
|
| 查看次数: |
57812 次 |
| 最近记录: |