0

My target a.csv and its content example is below:

2020-02,Platform,Framework,ID,"8.0,8.1,9,10",N/A,path
2020-03,Platform,Framework,ID,10,N/A,path
2020-04,Platform,Framework,ID,20,N/A,path

My command is below:

  $target_string | tr "," " "

I want to change each line of csv file to array list. When I change strings of line to array, I got some problems that I split string inside double quotes as well. Because my command is changing all the commas to " ". How do I escape commas inside double quotes when I change string to array in bash shell?

HxH
  • 77
  • 2
  • 11
  • Check https://stackoverflow.com/questions/7804673/escaping-separator-within-double-quotes-in-awk and https://stackoverflow.com/questions/8940352/how-to-handle-commas-within-a-csv-file-being-read-by-bash-script – Karthick Vinod Mar 20 '20 at 10:34
  • See also the freenode #bash factoid on CSV at http://wooledge.org/~greybot/meta/csv -- the last line of the file is the most recent version. – Charles Duffy Mar 20 '20 at 16:23

1 Answers1

0

This might not be the best way, but you could try to first replace the inner commas with something special like @@, then change them back to commas where you do your processing or whatver.

This code will loop multiple times until it will replace all the inner commas with @@, so not sure how fast it will be on big files

while [[ 1 == 1 ]]; do

    sed -i -E 's/"([^,]*),(.*)"/"\1@@\2"/g' ${file}

    if [[ $(grep "\".*,.*\"" ${file} | wc -l) == 0 ]]; then
        exit 0
    fi

done
robert.baboi
  • 354
  • 1
  • 5
  • In [How to Answer](https://stackoverflow.com/help/how-to-answer), note the section *Answer Well-Asked Questions", and therein the bullet point regarding questions "that have been asked and answered many times before". Nothing against adding your own answer -- but you should add it on the canonical instance of the question, so it's next to (and competing with) the existing ones there. – Charles Duffy Mar 20 '20 at 16:22