awk:用另一个文件过滤一个文件 [英] awk: filter a file with another file

查看:144
本文介绍了awk:用另一个文件过滤一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试用另一个文件过滤一个文件. 我有一个文件d3_tmp和m2p_tmp;它们如下:

I'm trying to filter a file with another file. I have a file d3_tmp and m2p_tmp; They are as follows:

$ cat d3_tmp 
0x000001     0x4d    2
0x1107ce     0x4e    2
0x111deb     0x6b    2

$ cat m2p_tmp 
mfn=0x000001 ==> pfn=0xffffffffffffffff
mfn=0x000002 ==> pfn=0xffffffffffffffff
mfn=0x000003 ==> pfn=0xffffffffffffffff

我想打印出m2p_tmp中第二行不等于d3_tmp第一行的行. (文件用\ t和=拆分)

I want to print out the lines in m2p_tmp whose second column is not equal to the first column of d3_tmp. (The files are split with \t and =)

所以期望的结果是:

  mfn=0x000002 ==> pfn=0xffffffffffffffff
  mfn=0x000003 ==> pfn=0xffffffffffffffff

但是,在我使用以下awk命令之后:

However, after I use the following awk command:

awk -F '[\t=]' ' FNR==NR { print $1; a[$1]=1; next } !($2 in a){printf "%s \t 0\n", $2}'     d3_tmp  m2p_tmp 

结果是:

0x000001  
0x1107ce  
0x111deb  
0x000001     0
0x000002     0
0x000003     0

我不确定为什么"a中的$ 2"不起作用. 有人可以帮忙吗?

I'm not sure why "$2 in a" does not work. Could anyone help?

非常感谢!

推荐答案

使用awk

awk 'NR==FNR{for (i=1;i<=NF;i++) a[$i];next} !($2 in a)' d3_tmp FS="[ =]" m2p_tmp

a [$ i]用于将文件d3_tmp中的所有项目收集到数组a中,NR == FNR用于控制收集仅集中在d3_tmp上. 在第二部分中,将FS设置为space或"=",比较文件m2p_tmp中的$ 2是否在此数组a中(如果在其中,则打印).

a[$i] is used to collect all items in file d3_tmp into array a, NR==FNR used to control the collection is only focus on d3_tmp. in second part, set the FS to space or "=", and compare if $2 in file m2p_tmp is in this array a or not, if in, print it.

问题已被编辑,因此我也必须更改代码.

The question has been edited, so I have to change the code as well.

awk 'NR==FNR{a[$1];next} !($2 in a)' d3_tmp FS="[ \t=]" m2p_tmp

这篇关于awk:用另一个文件过滤一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆