bash - sed: to merge contents of one CSV to another based on variable match -


attempting correlate geo ip data 1 csv access log of another.

sample lines of data:

csv1

bob,app1,8-jan-15,8.8.8.8 april,app3,2-jan-15,5.5.5.5 george,app2,1-feb-15,8.8.8.8 

csv2

8.8.8.8,us,united states,ca,california,mountain view,94040,america/los_angeles 5.5.5.5,us,united states,fl,florida,miami 

i want search csv1 ip listed in in csv2 , append fields 1,2,4 csv1 when ip matches.

so far have, i'm getting errors believe @ sed portion.

    #!/bin/bash  line in $( cat csv2 | awk -f',' '{print $1 "," $2 "," $4}' )         $ip = $( echo $line | cut -d, -f1 )         sed -i.bak "s/"$ip/\""$line\"" csv1 done 

desired output:

bob,app1,8-jan-15,8.8.8.8,united states,ca dawn,app3,2-jan-15,5.5.5.5,united states,fl george,app2,1-feb-15,8.8.8.8,united states,ca 

using join command:

$ join -t , -1 4 -2 1 -o 1.1,1.2,1.3,1.4,2.3,2.4 <(sort -t, -k4,4 csv1) <(sort -t, csv2) bob,app1,8-jan-15,8.8.8.8,united states,ca 

using sort overkill here, >1 line files, join requires files sorted on join key

with awk

$ awk -f, -v ofs=, 'nr == fnr {a[$1] = $3 ofs $4; next} $4 in {print $0, a[$4]}' csv2 csv1 bob,app1,8-jan-15,8.8.8.8,united states,ca 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -