简体   繁体   English

读取文件,通过标签解析,并使用Bash Script插入mysql数据库

[英]Read file, parse by tabs and insert into mysql database with Bash Script

I am doing a bash script to read a file line by line, parse and insert it in mysql database. 我正在做一个bash脚本,逐行读取文件,解析并将其插入mysql数据库。

The script is the following: 脚本如下:

#!/bin/bash

echo "Start!"

while IFS='     ' read -ra ADDR;
do
   for line in $(cat filename)
   do
      regex='(\d\d)-(\d\d)-(\d\d)\s(\d\d:\d\d:\d\d)'
      if [[$line=~$regex]]
      then
         $line='20$3-$2-$1 $4';
      fi
      echo "insert into table (time, total, caracas, anzoategui) values('$line', '$line', '$line', $
   done | mysql -uuser -ppassword database;
done < filename

The file 'filename' with data is something like this: 带有数据的文件“文件名”是这样的:

15/08/13 09:34:38       17528                                   5240    399     89      460     159     1107    33240
15/08/13 09:42:57       17528                                   5240    399     89      460     159     1107    33240
15/08/13 10:20:03       17492                                   5217    394     89      459     159     1101    33245
15/08/13 11:20:02       17521                                   5210    402     90      462     158     1112    33249
15/08/13 12:20:04       17540                                   5209    396     90      459     160     1105    33258

And its dropping this: 并删除此:

在此处输入图片说明

I would recommend creating your sql file using awk and then sourcing it from mysql . 我建议使用awk创建您的sql文件,然后从mysql采购它。 Re-direct the output once it looks ok to you into a new file (say insert.sql ) and source it from mysql command line. 将输出重新定向到一个新文件(例如insert.sql ),然后从mysql命令行获取它。 Something like this: 像这样:

$ cat file
15/08/13 09:34:38       17528                                   5240    399     89      460     159     1107    33240
15/08/13 09:42:57       17528                                   5240    399     89      460     159     1107    33240
15/08/13 10:20:03       17492                                   5217    394     89      459     159     1101    33245
15/08/13 11:20:02       17521                                   5210    402     90      462     158     1112    33249
15/08/13 12:20:04       17540                                   5209    396     90      459     160     1105    33258

$ awk -F'[/ ]+' -v q="'" '{print "insert into table (time, total, caracas, anzoategui) values ("q"20"$3"-"$2"-"$1" "$4q","q$5q","q$6q","q$7q");"}' file
insert into table (time, total, caracas, anzoategui) values ('2013-08-15 09:34:38','17528','5240','399');
insert into table (time, total, caracas, anzoategui) values ('2013-08-15 09:42:57','17528','5240','399');
insert into table (time, total, caracas, anzoategui) values ('2013-08-15 10:20:03','17492','5217','394');
insert into table (time, total, caracas, anzoategui) values ('2013-08-15 11:20:02','17521','5210','402');
insert into table (time, total, caracas, anzoategui) values ('2013-08-15 12:20:04','17540','5209','396');

Use the LOAD DATA statement. 使用LOAD DATA语句。 Do any transformations you have to on your file first, then 首先对文件进行任何转换,然后

LOAD DATA LOCAL INFILE 'filename' INTO table (time, total, caracas, anzoategui)

Tab separated fields is the default. 制表符分隔的字段是默认字段。

Bash uses POSIX character classes. Bash使用POSIX字符类。 So instead of \\d you could use [0-9] or [[:digit:]] to match a digit. 因此,您可以使用[0-9][[:digit:]]代替数字\\d来匹配数字。

Also, there must be spaces in between the brackets and operator in your regex test. 另外,在正则表达式测试中,括号和运算符之间必须有空格。 So [[$line=~$regex]] would be fixed by doing [[ $line =~ $regex ]] . 因此[[$line=~$regex]]可以通过[[ $line =~ $regex ]]

In order to access the regular expressions enclosed in parentheses, you must access the builtin array variable BASH_REMATCH . 为了访问括号中的正则表达式,必须访问内置数组变量BASH_REMATCH So, to access the first match, you'd reference ${BASH_REMATCH[1]} , the second ${BASH_REMATCH[2]} , and so on. 因此,要访问第一个匹配项,您将引用${BASH_REMATCH[1]} ,第二个${BASH_REMATCH[2]} ,依此类推。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM