I have a script that is comparing two files and outputting the values if a condition is met.The script works well if I reduce the records in my second file but fails if I use the actual file with 100000 records. What could I be doing wrong? Here is my code
awk -F '|' 'NR==FNR{a[$1"|"$2]=$3}NR!=FNR{if(exist a[$1"|"$3]) {print "update inventory set amount=" a[$1"|"$3] " where balance_id=" $2 " and balance_type_id=3019;"}}' cash.txt test.txt > update.sql
Cash.txt
0|3019|0
1|3019|1
2|3019|2
3|3019|3
4|3019|4
5|3019|5
6|3019|5
test.txt
0|0|3019
1|1|3019
2|2|3019
3|3|3019
4|4|3019
5|5|3019
6|6|3019
7|7|3019
8|8|3019
9|9|3019
10|10|3019
11|11|3019
12|12|3019
You may try this awk
:
awk -F '[|\r]' 'NR == FNR {
map[$1 "|" $2] = $3
next
}
$1 "|" $3 in map {
print "update inventory set amount=" map[$1 "|" $3] " where balance_id=" $2 " and balance_type_id=3019;"
}' cash.txt test.txt > update.sql
By not using next
in first block your awk is evaluating FNR != NR
condition for each record in first file and then for each record of second bigger file.
Also second block can be just $1 "|" $3 in map
so that we process only matching records.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments