1

I would like to join data from two CSV files based on matching column information. The data to match is from File1.csv column 5, and File2 column 1, and i want to append the information from File2 column 2 upon match, and if no match leave empty double quotes.

File1.csv

"Z","P","W","K","1","1.18.24.59"
"S","K","D","X","9","1.14.19.238"
"R","M","P","Y","8","1.15.11.21"
"B","D","0","U","5","1.9.20.159"
"R","E","W","Q","6","135.0.0.1"
"K","D","K","R","9","1.9.74.13"

File2.csv

"65.9.7.19","374 22 53"
"1.9.74.13","123 256 51"
"1.18.24.59","23 25 41"
"1.15.11.21","98 77 8291"
"1.14.19.238","8827 145 8291"
"1.9.20.159","283 1 5734"

Desired Output

"Z","P","W","K","1","1.18.24.59","23 25 41"
"S","K","D","X","9","1.14.19.238","8827 145 8291"
"R","M","P","Y","8","1.15.11.21","98 77 8291"
"B","D","0","U","5","1.9.20.159","283 1 5734"
"R","E","W","Q","6","135.0.0.1",""
"K","D","K","R","9","1.9.74.13","123 256 51"
1

Here's one solution, using awk. Tested on GNU awk 4.1.3.

$ awk -F, 'NR==FNR{a[$1]=$2}NR!=FNR{print $0","(a[$6]?a[$6]:"\"\"")}' file2.csv file1.csv
"Z","P","W","K","1","1.18.24.59","23 25 41"
"S","K","D","X","9","1.14.19.238","8827 145 8291"
"R","M","P","Y","8","1.15.11.21","98 77 8291"
"B","D","0","U","5","1.9.20.159","283 1 5734"
"R","E","W","Q","6","135.0.0.1",""
"K","D","K","R","9","1.9.74.13","123 256 51"
$
  • The NR==FNR{a[$1]=$2 matches the lines in file2, and adds them into an array, keyed on field #1.
  • The NR!=FNR then matches the lines in file1.
  • print $0"," prints the whole line from file1 followed by a comma.
  • (a[$6]?a[$6]:"\"\"") prints the corresponding contents of the array built earlier, but just "" if no entry found.
1

This turned out to be harder than I expected, but with the tools from the python-based csvkit version 1.0.2:

$ csvjoin -HI -c 6,1 --left File1.csv File2.csv 2>/dev/null | csvformat -K1 -U1
"Z","P","W","K","1","1.18.24.59","23 25 41"
"S","K","D","X","9","1.14.19.238","8827 145 8291"
"R","M","P","Y","8","1.15.11.21","98 77 8291"
"B","D","0","U","5","1.9.20.159","283 1 5734"
"R","E","W","Q","6","135.0.0.1",""
"K","D","K","R","9","1.9.74.13","123 256 51"

(The -I seems to be necessary to prevent csvjoin from guessing that the dot-separated fields are datetimes, at least in my locale.)

0
for i in `cat file2.csv | awk -F "," '{print $1}'| sed 's/"//g'`; do j=`sed -n '/'$i'/p' file2.csv| awk -F "," '{print $2}'| sed 's/"//g'`;awk -v i="$i" -v j="$j" -F "," '$NF==i{$7=j;print $0}' file1.csv; done| sed 's/ /","/g'| sed 's/^/"/g'| sed 's/$/"/g'

output

"Z","P","W","K","1","1.18.24.59","23 25 41"
"S","K","D","X","9","1.14.19.238","8827 145 8291"
"R","M","P","Y","8","1.15.11.21","98 77 8291"
"B","D","0","U","5","1.9.20.159","283 1 5734"
"R","E","W","Q","6","135.0.0.1",""
"K","D","K","R","9","1.9.74.13","123 256 51"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.