我有 2 个 nix 文件。每个文件中的所有数据都在一行中。每个值由空字符分隔。数据匹配中的一些值。
我如何将这些数据解析到一个只列出匹配值的新文件中?
我想我可以使用 sed 将空字符更改为换行符?从那以后我不太确定...
有什么想法吗?
最佳答案
使用tr
、sort
和comm
:
将空值转换为新行,并对结果进行排序:
$ tr '\000' '\n' < file1 | sort > file1.txt
$ tr '\000' '\n' < file2 | sort > file2.txt
然后使用 comm
获取两个文件共有的行:
$ comm -1 -2 file1.txt file2.txt
<lines shown here are the common lines between file1.txt and file2.txt>
关于linux - 比较 2 个 Unix 文件并将匹配行输出到一个新文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8722103/