0

I'm using bash shell. I want to parse one CSV file "A", search for that string in CSV file "B", and then output the value from the first column of the matching row in CSV file B. So I tried

while IFS=, read -r col1 name col3 col4 col5
do
    echo "I got:$name|$col5"
    value=`grep ${name} /tmp/accounts_created.csv | cut -d, -f${0}`
    echo "value:$value \n"
done < ~/Downloads/all_data.csv

but for some reason this isn't working out, despite the fact I know there is matching data in both files. Is there a better way to be doing this?

2
  • 1
    You didn't show any sample data. You should investigate the join command Sep 25, 2017 at 17:41
  • The better way is to use a CSV parser. For "small" data you might look f.i. at csvkit. Sep 25, 2017 at 19:42

1 Answer 1

0

As what I'm understanding, you only need awk.

awk -F, 'NR==FNR{seen[$2]=1;next} /seen[$2] ~ $0/{print $1}
' ~/Downloads/all_data.csv /tmp/accounts_created.csv
2
  • Does awk deal with CSV formatting? THat is, one can't always split on a "," with CSVs since some cells may contain commas in them.
    – Dave
    Sep 25, 2017 at 17:41
  • Yes, so we need sample input(s) and your expected output Sep 25, 2017 at 17:43

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.