【请教】日志提取指定字段
背景:每天要处理10G的log文件,格式如下:
name=xxx age=yy date=yyyymmdd Pc=xxxx (可能还有其他字段,awk NF会变)
现在要提取age=yy Pc=xxxx这样的内容,我用grep 写了一个脚本,很慢!
#! /bin/bash
#set -x
# the default delimiter
fs=" "
ret=""
# get options
while getopts F:k: opt
do
case "$opt" in
F)
fs=$OPTARG;;
k)
keys=$OPTARG;;
esac
done
# split string into an array
keys=(${keys//,/ })
num=${#keys[@]}
if [ $num -eq 0 ];
then
echo "not enough parameter";
exit;
fi
while read line
do
retstr=""
found=0
for key in ${keys[@]}
do
# \bxxx : word start by xxx
tmpstr=`echo ${line} | grep -oE "\b${key}=[^${fs}]*" `
# do only when string is not empty
[ -n "${tmpstr}" ] && retstr=${retstr}${tmpstr}${fs} && let found+=1
done
# check num
[ -n "${retstr}" -a $found -eq $num ] && echo -e ${retstr}"\n" | sed '/^$/d'
done
该怎样做呢?求大牛们指教!