i using awk in windows. have script called test.awk. script should read file , replace filed (key) value. key->value list in file called translate.txt.
it's structure this:
e;emil f;friedrich g;gustaf h;heinrich i;ida in simple example, input file be
e,111 f,222 g,333 h,444 i,555 .. so output should be
emil,111 friedrich,222 gustaf,333 heinrich,444 ida,555 .. the script having using user function key2value replacement, don't succeed in giving function file translate.txt source. see code:
{ fs="," d=key2value($1) print d "," $2 } function key2value(b) { #this should use file, not processed 1 filename="translate.txt" begin { fs=";" if ($1=b) { return $2 } end } another thing, fs buggy to, starts working second line only.
this simple one-liner trick:
awk 'fnr==nr{a[$1]=$2;next}{print a[$1],$2}' fs=',|;' ofs=',' translate input emil,111 friedrich,222 gustaf,333 heinrich,444 ida,555 in script form:
begin { # begin block executed before files read fs="[,;]" # set fs either comma or semi-colon ofs="," # set ofs (output field separator) comma } fnr==nr { # fnr==nr true when reading first file key2value[$1]=$2; # create associative array of key,value pairs next # grab next line in first file } { # in second file, print looked value , $2 print key2value[$1],$2 } run like:
awk -f translate.awk translate.txt input.txt there numerous error script, should take read of effective awk programming.
Comments
Post a Comment