这个程序是实习的时候,对于实际项目中对csv文件进行提取并插入数据库中的实际操作
-
#!/bin/bash
-
#日志文件的时间格式
-
DATE=$(date +%Y-%m-%d)
-
#日志文件内部时间格式
-
DATE1=$(date +%c)
-
#判断脚本后面是否有文件名
-
if [ $# -eq 0 ];then
-
echo "Usage:$0 "
-
echo "Usage:$0 $DATE1" >> Import.log.$DATE
-
exit 1
-
elif [ $# -eq 1 ];then
-
#将文件名赋值与FILENAME
-
FILENAME=$1
-
#取得文件名.前面的部分
-
NAME=${FILENAME%.*}
-
#使用AWK来提取文件并将提取的信息重定向到临时文件tt.txt中
-
awk -F "," 'NR > 2{
-
printf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\", \"%s\"\n",$1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17);
-
}' $FILENAME > tt.txt
-
echo "awk awk -F "," 'NR > 2{printf("\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\"%s\",\"%s\",\"%s\", \"%s\",\"%s\",\"%s\",\"%s\",\"%s\"\n",$1, $2, $3, $4, $5, $6, $7, $8, $9,$10, $11, $12, $13, $14, $15, $16, $17);}' $FILENAME > tt.txt $DATE1" >>Import.log.$DATE
-
#使用sed来处理提取csv时产生的字符不匹配的问题
-
sed 's/^M//g' tt.txt>$NAME.txt
-
echo "sed 's/^M//g tt.txt' $DATE1" >> Import.log.$DATE
-
#连接数据库
-
db2 "connect to $DBNAME user $DBUSER using $DBPWD"
-
echo "db2 connect to $DBNAME user $DBUSER using $DBPWD $DATA1" >> Import.log.$DATE
-
-
#向数据库表中插入提取的文件
-
db2 "import from $NAME.txt of del insert into T_TFS_TASKINFO"
-
echo "db2 "import from $NAME.txt of del insert into T_TFS_TASKINFO"">>Import.log.$DATE
-
#断开连接
-
db2 "connect reset"
-
echo "db2 connect reset $DATA1" >>Import.log.$DATE
-
#删除临时文件tt.txt
-
rm -f tt.txt
-
echo "rm -f tt.txt $DATA1" >>Import.log.$DATE
-
else
-
echo "Usage:$0 "
-
echo "Usage:$0 $DATE1" >> Import.log.$DATE
-
exit 1
-
fi
阅读(9408) | 评论(0) | 转发(0) |