繁体   English   中英

使用Shell脚本解析日志值

[英]Parsing log values with shell script

我正在尝试编写一个shell脚本来解析日志的grepped行中的值:

 <WhereIsTheCar - the car with id number 'Sys Generated. VARIABLESTRING 1111' is driving to: Canada>
 <WhereIsTheCar - the car with id number 'Sys Generated. VARIABLESTRING 2222' is driving to: Mexico>
 <WhereIsTheCar - no car could be found with the following ID number: 'Sys Generated. VARIABLESTRING 3333'>

我已经为这些行添加了代码并创建了一个数组。 然后,我正在寻找类似以下内容的输出:

Canada
    Sys Generated. VARIABLESTRING 1111

Mexico
    Sys Generated. VARIABLESTRING 2222

Not Found
    Sys Generated. VARIABLESTRING 3333

我承认我不太擅长shell脚本编写,但是我想出了一种“蛮力”方法来获取所需的值:

i=0
for line in "${grep[@]}"
do
    loc[i]=`sed -e "s/.*\:\(.*\)>/\1/" <<< $line | sed -e "s/^[ \t]*//" -e "s/[ \t]*$//" -e "s/^\([\"']\)\(.*\)\1\$/\2/g"`
    echo ${loc[i]};
    id[i]=`sed -e "s/^.*\'\(.*\)\'.*$/\1/" <<< $line | sed -e "s/^[ \t]*//" -e "s/[ \t]*$//" -e "s/^\([\"']\)\(.*\)\1\$/\2/g"`
    echo ${id[i]};
    let i++
done

我在哪里创建位置和ID数组,然后尝试修剪空白和多余的引号。 我想我可以从这里结束,但是我想知道是否有人有一个更优雅(或更适合)的方法。 任何意见,将不胜感激。

另一种可能性是在bash中使用BASH_REMATCH而不是awksed

   BASH_REMATCH
          An  array  variable  whose members are assigned by the =~ binary
          operator to the [[ conditional command.  The element with  index
          0  is  the  portion  of  the  string matching the entire regular
          expression.  The element with index n  is  the  portion  of  the
          string matching the nth parenthesized subexpression.  This vari‐
          able is read-only.

所以这应该为你工作

#!/bin/bash
while read -r line; do
  [[ $line =~ "is driving to:"(.*)">" ]] && echo ${BASH_REMATCH[1]} || echo "Not Found"
  [[ $line =~ \'(.*)\' ]] && echo -e "\t${BASH_REMATCH[1]}\n"
done < "file"

输出示例

> ./abovescript
Canada
    Sys Generated. VARIABLESTRING 1111

Mexico
    Sys Generated. VARIABLESTRING 2222

Not Found
    Sys Generated. VARIABLESTRING 3333

awk会更容易:

awk -F"('|driving to: |>)" '{printf "%s\n\t%s\n\n", NF==5?$4:"Not Found",$2;next}' file

测试您的数据:

kent$  cat f
<WhereIsTheCar - the car with id number 'Sys Generated. VARIABLESTRING 1111' is driving to: Canada>
<WhereIsTheCar - the car with id number 'Sys Generated. VARIABLESTRING 2222' is driving to: Mexico>
<WhereIsTheCar - no car could be found with the following ID number: 'Sys Generated. VARIABLESTRING 3333'>

kent$  awk -F"('|driving to: |>)" '{printf "%s\n\t%s\n\n", NF==5?$4:"Not Found",$2;next}' f
Canada
        Sys Generated. VARIABLESTRING 1111

Mexico
        Sys Generated. VARIABLESTRING 2222

Not Found
        Sys Generated. VARIABLESTRING 3333

使用sed

sed -nr "/driving to/ s/.*'([^']+)'.*:(.*)>/\2\n\t\1/p; /no car could be found/ s/.*'([^']+)'.*/ Not Found\n\t\1/p" file

 Canada
        Sys Generated. VARIABLESTRING 1111
 Mexico
        Sys Generated. VARIABLESTRING 2222
 Not Found
        Sys Generated. VARIABLESTRING 3333

说明:

分为两部分,直接处理输入文件,无需循环。

提示:需要在sed中处理单个配额时,请使用双配额。

/driving to/ s/.*'([^']+)'.*:(.*)>/\\2\\n\\t\\1/p用于获取发现汽车的内容/no car could be found/ s/.*'([^']+)'.*/ Not Found\\n\\t\\1/p用于表示未找到汽车的内容。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM