I'm a complete newbie to awk and am wondering how to process information as following:mapping.txt:80  001 00281  011 012 013 01482  021 022...input.txt:81  10382304480  10382305481  103823064...Desired output.txt:103823044|011|103823044|012|103823044|013|103823044|014|103823054|001|103823054|002|103823064|011|103823064|012|103823064|013|103823064|014|I've done simple mapping wherein the column numbers are fixed but I'm unsure of how to map a dynamic number of columns to the desired output
Using awk to process a mapping file with dynamic number of columns
awk;gawk
You can try following:awk 'NR == FNR { x[$1,$2] = $1; next; } { for (i in x) { if (x[i] == $1) { split(i,t,SUBSEP); for (z = 2; z <= NF; z++) { print t[2] | $z |; } } } }' input.txt mapping.txtThe first block is executed while reading the file input.txt. It is achieved by condition NR == FNR which is true while the first file is read. In the first block we create an array x with keys from first ans second column and values from first column. The second block is executed for the file mapping.txt because of next in the first block. In the second block we check for every key in array x if value from column 1 exist as value in array x, and if exists extract second part of the key with split and we print in a loop this value and values from second column till last column (NF - number of fields in current record).