Read a json message from CSV file without removing

2019-09-21 04:20发布

问题:

This question is an exact duplicate of:

  • jq - bash script for csv to json with arrays? 2 answers

I have a csv file, i need to read the rows one by one without removing any char like , " ; etc

in.csv

{ "customer": { "name": "abc", "email":"mailme2@xxx.com","account_classification": "xyz"} }    
{ "customer": { "name": "def", "email":"mailme2@xxx.com","account_classification": "xyz"} }    
{ "customer": { "name": "g,h", "email":"mailme2@xxx.com","account_classification": "xyz"} }

My code

declare -a demo=($(cat in.csv))
for i in "${demo[@]}"
do
echo "$i"
done

Output (expected output)

{ "customer": { "name": "abc", "email":"mailme2@xxx.com","account_classification": "xyz"} }    
{ "customer": { "name": "def", "email":"mailme2@xxx.com","account_classification": "zyx"} }

Output i am getting like (output with above code):

"{ ""customer"": { ""name"": ""abc"", ""email"":""mailme2@xxx.com"",""account_classification"": ""xyz""} }" "{ ""customer"": { ""name"": ""def"", ""email"":""mailme2@xxx.com"",""account_classification"": ""xyz""} }" "{ ""customer"": { ""name"": ""g,h"", ""email"":""mailme2@xxx.com"",""account_classification"": ""xyz""} }

Can someone give me sample code?

回答1:

You can use something like

while IFS= read -r line; do
   printf "%s\n" "${line}"
done < in.csv

or when you want to change lines in a future improvement

sed 's/future_change/new_content/g' in.csv


标签: bash shell csv