fairyguo 发表于 2018-10-27 08:01:54

实战6 Nginx的web日志导入到MongoDB数据库

#!/bin/bash  
file=/usr/local/nginx/logs/2014/06/access_20140612.log
  
dir=/usr/local/nginx/logs/ng_mongodb
  
function fenxi () {
  
cat $file | awk -F "uid=" '{print $2}' | awk '{print $1}' | grep -v "^$" | awk '{print "\""$0"\""}' > ${dir}/cookieId.log
  
cat $file | awk '{print $4}' | awk '{print "\""$0"\""}' >${dir}/ip.log
  
cat $file | awk '{print $1}'| awk '{print "\""$0"\""}' > ${dir}/appKey.log
  
cat $file | awk '{print $2}' | awk -F "[" '{print $2}'| awk '{print "\""$0"\""}' > ${dir}/visitdate.log
  
cat $file | awk -F "\"" '{print $6}'| awk '{print "\""$0"\""}' >${dir}/browsertype.log
  
cat $file | awk -F "\"" '{print $4}'| awk '{print "\""$0"\""}' > ${dir}/referer.log
  
cat $file | awk -F "\"" '{print $6}' |awk '{print $1}'| awk '{print "\""$0"\""}'> ${dir}/os.log
  
cat $file | awk -F "\"" '{print $2}' | awk -F "=" '{print $2}'|awk -F "&" '{print $1}'|awk '{print $1}' >${dir}/videoId.log
  
}
  
if [ -f $file ];then
  
      if [ -d $dir ];then
  
                fenxi
  
      else
  
                mkdir $dir
  
                fenxi
  
      fi
  
else
  
      echo "sorry! the directory "/usr/local/nginx/logs/access.log" is not exsit"
  
fi
  
#截取后的日志文件如下
  
# ll
  
total 1612
  
-rw-r--r-- 1 root root42528 Jun 17 18:18 appKey.log
  
-rw-r--r-- 1 root root 330977 Jun 17 18:18 browsertype.log
  
-rw-r--r-- 1 root root 111615 Jun 17 18:18 cookieId.log
  
-rw-r--r-- 1 root root58954 Jun 17 18:18 ip.log
  
-rw-r--r-- 1 root root46791 Jun 17 18:18 os.log
  
-rw-r--r-- 1 root root 121244 Jun 17 18:18 referer.log
  
-rw-r--r-- 1 root root 804258 Jun 17 18:18 test1.dat
  
-rw-r--r-- 1 root root10275 Jun 17 18:18 videoId.log
  
-rw-r--r-- 1 root root81512 Jun 17 18:18 visitdate.log
  
#然后把以上八个文件汇总成一个文件并用特殊符号分开
  
注意:说到这里,我得感谢 小武的帮忙 paste命令
  
# man paste
  
格式:
  
paste -d -s file1 file2
  
参数
  
    -d:指定不同于空格或tab键的域分隔符。例如用@分隔域,使用- d @
  
    -s:将每个文件合并成行而不是按行粘贴
  
paste的用法:http://os.chinaunix.net/a2008/0921/985/000000985401.shtml
  
#合并到一个文件中并用逗号隔开
  
# paste -d , browsertype.log cookieId.log appKey.log ip.log os.log referer.log videoId.log visitdate.log > test1.dat


页: [1]
查看完整版本: 实战6 Nginx的web日志导入到MongoDB数据库