分享一个提取字段的脚本

公司做统计提取字段的服务是C编译出的bin文件,经常core dump导致机器挂断,深受其苦,自己写了一个shell版的作为替代。

本来打算用awk实现,

 awk -F "[= ]" ‘{do something}’
这样,可是日志字段不固定(可能出现,可能不出现),不太好实现,就该用grep了,每两个字段出现的前后顺序倒是固定的。

脚本如下(get_fields.sh):

#! /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

使用实例:

test.log:

a=1 b=2 c=3 d=a

要提取出a,c两个字段:

cat test.log | sh get_fields.sh -F " " -k a,c
结果:

a=1 c=3


最后,感谢亚杰对getopts的技术支持!感谢正则三十分钟入门教程!


------------

更新:

在ChinaUnix上发了贴,问了这个问题,大牛们的回答太给力了!!!

地址:http://bbs.chinaunix.net/thread-4169843-1-1.html

最终,我还是采用了awk:

cat test.log | grep -E "\bname" | awk '{for(i=1;i<=NF;i++)s=($i~/^name=|^Pc=/)?s"\t"$i:s;$0=s;s=""}NF+=0'




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值