2014-01-24 127 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用於取得沒有找到車的內容。