解析日誌值,我想寫一個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數組,然後試圖修剪掉空白和額外的引號。我想我可以從這裏完成,但我想知道是否有人有更優雅(或更適合)的方法。任何意見,將不勝感激。