擅长:python、mysql、java
<p>另一个在awk中,这次使用<code>split</code>:</p>
<pre><code>$ awk 'NR==FNR{a[$1]=$2;next}{split($1,b,"_");if(i=b[1] in a)print $1,a[b[1]]}' file1 file2
utt1_1 f
utt1_2 f
utt2_1 f
utt2_2 f
utt2_3 f
utt3_1 m
utt4_1 f
utt4_2 f
utt4_3 f
</code></pre>
<p>说明:</p>
<pre><code>NR==FNR { # process the first file
a[$1]=$2 # hash it to a hash with $2 as value
next } # move to next record
{ # process the second (or any number of) file
split($1,b,"_") # split the $1 on _
if(i=b[1] in a) # first part of b is the key to hash
print $1,a[b[1]] } # output $1 and hash value
' file1 file2
</code></pre>