2套/文件之间提取唯一值 [英] extracting unique values between 2 sets/files
本文介绍了2套/文件之间提取唯一值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在linux / shell的环境工作,我怎么能做到以下几点:
文本文件1包含:
1
2
3
4
五
文本文件2包含:
6
7
1
2
3
4
我需要提取在文件2中的条目不属于在在本实施例文件1.因此,'6'和'7'。
我如何做到这一点的命令行?
非常感谢!
解决方案
$ AWK'FNR == {NR一个[$ 0] ++;接下来}!一[$ 0]'文件1文件2
6
7
如何code ++工程的说明:
- 如果我们在文件1工作,跟踪我们看到的每一行文本。
- 如果我们正在努力file2中,并没有看到该行的文字,然后打印。
的细节说明:
-
FNR
是当前文件的记录号 -
NR
是从所有输入文件的当前总战绩号 -
FNR == NR
只有当我们在阅读文件1真 -
$ 1,0
是文本的当前行 -
A [$ 0]
与按键文本的当前行 哈希 -
A [$ 0] ++
跟踪,我们已经看到文本的当前行 -
!一个[$ 0]
是真实的,只有当我们还没有看到该行文本 - 打印文本行,如果上面的图案返回true,这是默认的awk行为时,给出没有明确的行动
Working in linux/shell env, how can I accomplish the following:
text file 1 contains:
1
2
3
4
5
text file 2 contains:
6
7
1
2
3
4
I need to extract the entries in file 2 which are not in file 1. So '6' and '7' in this example.
How do I do this from the command line?
many thanks!
解决方案
$ awk 'FNR==NR {a[$0]++; next} !a[$0]' file1 file2
6
7
Explanation of how the code works:
- If we're working on file1, track each line of text we see.
- If we're working on file2, and have not seen the line text, then print it.
Explanation of details:
FNR
is the current file's record numberNR
is the current overall record number from all input filesFNR==NR
is true only when we are reading file1$0
is the current line of texta[$0]
is a hash with the key set to the current line of texta[$0]++
tracks that we've seen the current line of text!a[$0]
is true only when we have not seen the line text- Print the line of text if the above pattern returns true, this is the default awk behavior when no explicit action is given
这篇关于2套/文件之间提取唯一值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文