我是贝壳脚本的新蜜蜂,想知道是否有人可以帮助我满足以下要求 我需要编写一个shell脚本代码,它将从csv文件中读取下面的表并生成sql insert脚本 以下是从csv文件中提取的数据
Employee,Employee_no,Name,Depart_code,PF No,Salary
Start_employee,,,,,
,234,kasdjf,dev,27,394899
,233,ghasdjf,tes,25,294845
,235,dfsdjf,des,28,404899
end_employee,,,,,
,,,,,
Department,depart_code,depart_name,,,
Start_department,,,,,
,dev,development,,,
,tes,testing,,,
,des,design,,,
end_department,,,,,
答案 0 :(得分:0)
以下是如何使用awk
命令在分割csv的文件的for
循环中解决此问题:
#!/bin/bash
INPUTCSV="$1"
if [[ ! -f "${INPUTCSV}" ]] ; then
echo error: no input file
exit 10
fi
PID=$$
#remove everything before start:
rm -i file*txt
echo "" > insert.sql
# split data in ordered list of file
awk -v f=1 -v r=$PID '/^START_|^END_/{f;++f;next}
{print $0 >> "file_"f"" r".txt";}' "${INPUTCSV}"
MODE="TABLE"
# ordered loop on the above files
for datafile in $(find . -type f -name "file_?${PID}.txt"|sort -n) ; do
# we start with table definition:
if [[ "${MODE}" == "TABLE" ]] ; then
awk -F"," '{
{x=""; printf "insert into "$1" (\"" }
for(i=2 ; i<=NF; i++) {
if ($i != "") { printf x$i ; x="\",\"" }
}
print "\")"
}' $datafile >> insert.sql
# switch mode for each file
MODE="DATA"
else
awk -F"," -v sel=" select " '{
{x=""; printf sel "~" }
for(i=2 ; i<=NF; i++) {
if ($i != "") { printf x$i ; x="~,~" }
}
sel="union select "
print "~ from dual"
}' $datafile | tr "~" "'" >> insert.sql
echo ";" >> insert.sql
# switch mode for each file
MODE="TABLE"
fi
done
# remove possible empty inserts:
cat insert.sql |sed '/^insert into \(\)/d' > insert_final.sql
我必须在上面的代码中添加许多技巧来动态生成插入。向你解释会有点长。问题是你应该学会将你的大问题分成更小的部分。
以下是您输入的结果:
insert into Employee ("Employee_no","Name","Depart_code","PF No","Salary")
select '234','kasdjf','dev','27','394899' from dual
union select '233','ghasdjf','tes','25','294845' from dual
union select '235','dfsdjf','des','28','404899' from dual
;
insert into Department ("depart_code","depart_name")
select 'dev','development' from dual
union select 'tes','testing' from dual
union select 'des','design' from dual
;