指定的字符串是图片中红线框起来的部分需要用正则表达式,因为需要每隔一个小时执行一次shell,所以每次执行完shell后的还需要将txt的日志文件先MV到一个备份目录下,防止下次执行的时候又全量读一次。
指定目录暂时用/root/,备份目录暂时用:/root/ng_log_bak,文件名暂时用:access.log,mysql的用户名暂时用user,密码暂时用passwd(如果需要地址就写127.0.0.1,端口8080),mysql表名暂时用table_name ,
#!/bin/sh -e
LOGFILE=/root/access.log
BAKDIR=/root/ng_log_bak
TMPSQL=/tmp/altmp.sql
if [ -f ${LOGFILE} ]; then
cat ${LOGFILE} | gawk -F' ' '{ print "insert into table_name(col1,col2) values(`"substr($4,2)"`,`"gensub(".*?act=","","g",$13)"`);"; }' > ${TMPSQL}
mysql --user=user --password=passwd db_name < ${TMPSQL}
rm ${TMPSQL}
mv ${LOGFILE} ${BAKDIR}/access.log.$(date +%Y%m%d%H%m)
fi追问
LOGFILE=/root/access.log
BAKDIR=/root/ng_log_bak
TMPSQL=/tmp/altmp.sql
if [ -f ${LOGFILE} ]; then
cat ${LOGFILE} | gawk -F' ' '{ print "insert into table_name(col1,col2) values(`"substr($4,2)"`,`"gensub(".*?act=","","g",$13)"`);"; }' > ${TMPSQL}
mysql --user=user --password=passwd db_name < ${TMPSQL}
rm ${TMPSQL}
mv ${LOGFILE} ${BAKDIR}/access.log.$(date +%Y%m%d%H%m)
fi追问
非常感谢,根据您的提示跑出来
06 Mar 2014 22:30:19|label
06 Mar 2014 22:30:19|label
06 Mar 2014 22:30:19|label
06 Mar 2014 22:30:19|label
这样格式的文件,还想问您一下怎么才能把06 Mar 2014 22:30:19|label的格式变成2014-03-06 22:30:19|label呢?
温馨提示:答案为网友推荐,仅供参考
第1个回答 2014-03-12
#!/bin/sh
dir='/root'
dirbak='/root/ng_log_bak'
cd $dir
paraone=`cat $dir/access.log|awk -F'[' '{printf $2}'|awk '{printf $1 "\n"}'`
paratwo=`cat $dir/access.log|awk -F'=' '{printf $2}'|awk '{printf $1 "\n"}'`
sqlplus -s user/passwd@SID << EOF > insertoracle.log
insert into table_name(dateone,labelone) values('$paraone','$paratwo');
EOF
mv $dir/access.log $dirbak
dir='/root'
dirbak='/root/ng_log_bak'
cd $dir
paraone=`cat $dir/access.log|awk -F'[' '{printf $2}'|awk '{printf $1 "\n"}'`
paratwo=`cat $dir/access.log|awk -F'=' '{printf $2}'|awk '{printf $1 "\n"}'`
sqlplus -s user/passwd@SID << EOF > insertoracle.log
insert into table_name(dateone,labelone) values('$paraone','$paratwo');
EOF
mv $dir/access.log $dirbak