开发者

in shell script,how to read csv file line by line and get values

开发者 https://www.devze.com 2023-03-19 09:30 出处:网络
i have csv file with three fields(length,height,width).i want to read these values line by line and update these values in database.how to read the file line by line and update these values in data ba

i have csv file with three fields(length,height,width).i want to read these values line by line and update these values in database.how to read the file line by line and update these values in data base.

we can use "load In file" query to update entire file to db.but i want to update the modified values from csv to db.so how to read file 开发者_StackOverflowline by line and update these values.?


As Douglas said in a comment, using sh to parse CSV is a really non-scalable plan. If the input file is anything but trivial, it will not work. However, for trivial input, you just need to set IFS. Here's an example that reads and tries to put the data into a MySQL database (I didn't test the SQL...the syntax may be (probably is) off):

#!/bin/sh
IFS=,

while read one two three; do
    echo "insert into table( col1, col2, col3 ) "'
        '"values ('$one', '$two', '$three');"
done |
mysql db;
0

精彩评论

暂无评论...
验证码 换一张
取 消