awk - Lookup and Append -
guys can me awk script, i've been thinking of using nr=fnr method , substring doesn't work
here input
file 1.txt
bscrnc|cellname|cellid bbir1|bir004g+aw_geut|bi3004a bbir1|bir004g+aw_geut|bi3004b bbir1|bir004g+aw_geut|bi3004c bbir1|bir005g+lingong|bi3005a bbir1|bir005g+lingong|bi3005b
file2.txt
mb2|mbo bp5|bpd jh1|jho tn5|ttn tk3|tknj kt5|ktn lg4|lgs bi3|**bir** bh0|bih
the output
bscrnc|cellname|cellid|sitename bbir1|bir004g+aw_geut|bi3004a|bir004 bbir1|bir004g+aw_geut|bi3004b|bir004 bbir1|bir004g+aw_geut|bi3004c|bir004 bbir1|bir005g+lingong|bi3005a|bir005 bbir1|bir005g+lingong|bi3005b|bir005
we @ third field first 3 words bi3 in 1st file, see in 2nd file bi3->bir , append in fourth field , last 3 letter/words taken next 3 words 1st file
thank much!
it looks on right track (though, pointed out here, more people might have helped sooner if had given more details tried , thought problem was).
bl.awk
begin { fs = "|"; ofs = fs } # build mapping first file (file2.txt) nr == fnr { sitemap[$1] = $2; next; } # print header , rows second file (file1.txt) fnr == 1 { $4 = "sitename"; print; } fnr > 1 { sitekey = substr($3, 1, 3); sitevalue = sitemap[sitekey]; sitesuffix = substr($3, 4, 3); print $1, $2, $3, sitevalue sitesuffix; }
note want put file2.txt first on command line can build map before printing rows file1.txt:
bash-3.2$ awk -f bl.awk file2.txt file1.txt bscrnc|cellname|cellid|sitename bbir1|bir004g+aw_geut|bi3004a|bir004 bbir1|bir004g+aw_geut|bi3004b|bir004 bbir1|bir004g+aw_geut|bi3004c|bir004 bbir1|bir005g+lingong|bi3005a|bir005 bbir1|bir005g+lingong|bi3005b|bir005
Comments
Post a Comment