最近扒了学校图书馆的一堆信息,这些信息都是一个个的网页文本,想抽取其中有用的放到数据库中
每个网页中都有学生的学号,姓名,性别,校园卡号,身份证号,借书次数,欠款金额等信息,匹配抽取整起!
思路是这样:单独对每个网页抽取信息,放到文本文件的单独一行,用空格隔开,最后再导入数据库中
尝试着使用了awk,sed,grep等,最后发现用个grep就行了,简单嘛脚本如下
#!/bin/bash
#this script is used to extract information from text file
for i in $(ls /home/zlidentify/njupt/)
do
input=$(cat /home/zlidentify/njupt/$i)
num=$(echo $input | grep -o '[0-9]\{15,18\}\|[A-Z][0-9]\{8\}\|男\|女\|..
..:您好\|...册次')
echo $num >> /home/zlidentify/test/ok.txt
done
其中grep -o表示只输出匹配项
-o, --only-matching
Print only the matched (non-empty) parts of a matching line,
with each such part on a separate output line.
紧接着的是正则表达式,这里会匹配15到18个数字的字符串,以大写字母开始且后面接着8个数字的字符串等等
这是一行匹配的结果
>xxx:您好 B120101xx 男 110201200xxx400 3702841993102xxxx n>0册次
基本完成工作!
但是效果还不是很好,接着修改
awk -F ':您好' ‘{print $1 $2}’ | sed 's/^[0-9]\|>//g' text.txt > haha.txt
上面是去掉:您好,》等符号的正则匹配操作
哈哈,成功!
sed 1,4d new.txt >temp
删掉前面的空行
启动mysql
load data local infile '/home/zlidentify/test/temp' into table stu;
导入数据库成功!