I am trying to mention varaibel in AWK. I try a few I used to use earlier, but can't figureout where I am doing wrong...
cat tld.csv
co.uk
com
code
while read -r ip
do
awk -v i="$ip" -F, '$1 ~ /i$/ {print $0}' full-database.csv > tld-specific-database/$ip.csv
done < tld.csv
cat full-database.csv
a.com,bbb
co.uk,ddd
a.comi,bbb
c.ukip,ddd
Expected result
cat co.uk.csv
co.uk,ddd
cat com.csv
a.com,bbb
what i am getting now is
cat co.uk.csv
a.comi,bbb
cat com.csv
a.comi,bbb
What you want to match is the value of variable i, not the actual character (or string) i:
awk -F, -v i="$ip" '$1 ~ i "$"' db.csv >$ip.csv
or slightly terser include the $ in the variable value
awk -F, -v i="$ip$" '$1 ~ i' db.csv >$ip.csv
(either way the default action is print $0
so you can leave that out).
But you can do this in one pass almost as https://stackoverflow.com/users/3422102/david-c-rankin says, if the number of patterns in tld.csv
is reasonably small as you show, and this is more efficient if fulldb.csv
is large (which you don't show, but 'full' suggests):
awk -F, 'NR==FNR{x[$0];next} { for(i in x) if($1 ~ i "$") print > i ".csv" }' tld.csv fulldb.csv
or if you don't like the NR==FNR
trick
awk -F, 'BEGIN{while((getline<"tld.csv")>0) x[$0]} { for(i in x) ... }' fulldb.csv