I have the below index file
key1|1|1001
key1|1|2001
key2|2|3001
key2|2|4001
using this index file, I have to update my main file
key1|1000|2000|3000|4000
key2|1000|2000|3000|4000
The expected output should be
key1|1001|2000|3000|4000
key1|2001|2000|3000|4000
key2|1000|3001|3000|4000
key2|1000|4001|3000|4000
But my below script.awk is not duplicating the keys in the main file, instead it keeps overwriting the value in the respective index. What is the wrong with the script? awk -f script.awk index.txt main.txt
#!/bin/awk
BEGIN {
FS = "|"
}
( NR == FNR ) {
lookup[toupper($1)] = $0
}
( NR > FNR ) {
key = toupper($1)
split(lookup[key], replacements, "|")
for (i = 1; i <= NF; i++)
col[i] = $i;
for (i=1; i <= NF; i=i+1){
j=replacements[i]
col[j] = replacements[i+1]
}
for (i = 1; i <= NF; i++)
printf "%s|", col[i]
}