Issue
I'm trying to generate MySQL file from CSV data
90927744|1356976814398|0|1356976815339|18563182571|18563182571|18568753009|18563574221
is there is a way that I could use awk or sed to generate mysql insert statement as follows
insert into table (id,value,status,starttime,endtime,number,phone,number) values (90927744,1356976814398,0,1356976815339,18563182571,18563182571,18568753009,18563574221);
Thank you
Solution
To extract a particular field from your input....
$echo "90927744|1356976814398|0|1356976815339|18563182571|18563182571|18568753009|18563574221" | awk -F "|" '{print $4}'
1356976815339
If you have one line in a.txt
(see the script below for multiple lines in the input file)
$cat a.txt | awk -F "|" '{print $4}'
So to output the query you want--
$cat a.txt | awk -F "|" '{printf("insert into table (id,value,status,starttime,endtime,number,phone,number) values (%d, %d, %d, %d, %d, %d, %d, %d) \n", $1, $2, $3, $4, $5, $6, $7, $8)}'
If your file has multiple lines, then use the following script (or adapt this to your needs)
while read line
do
echo "$line" | awk -F "|" ........
done < a.txt
Answered By - Bill