2014-01-24 107 views
0

解析日志值,我想写一个shell脚本,从日志的grepped线分析值:与shell脚本

<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'> 

我已经grepped那些线条和创建阵列。那么我希望得到的输出是这样的:

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数组,然后试图修剪掉空白和额外的引号。我想我可以从这里完成,但我想知道是否有人有更优雅(或更适合)的方法。任何意见,将不胜感激。

回答

2

另一种可能是刚刚在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 
1

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 
0

使用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 

说明:

拆分两个部分,在输入文件directl上工作Ÿ,不需要循环。

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

/driving to/ s/.*'([^']+)'.*:(.*)>/\2\n\t\1/p用于取车的内容 /no car could be found/ s/.*'([^']+)'.*/ Not Found\n\t\1/p用于取得没有找到车的内容。