1
votes

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

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 =)

So the desired result is:

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

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 

The result is:

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

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

Thank you very much!

1
There is a trailing space. 0x000002 != 0x000002 - William Pursell
Thank you so much William! I got it! - Mike
You changed the topic, the source file is in different format now. Can you discuss in new request ? Otherwise, it will confuse others, if they see the solution which were provided. - BMW

1 Answers

2
votes

Using awk

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

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