我正在尝试编写一个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'>
我已经为这些线路做了一个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数组,然后尝试修剪空白和额外的引号。我想我可以从这里完成,但我想知道是否有人有更优雅(或更适合)的方法。任何意见,将不胜感激。
答案 0 :(得分:2)
另一种可能性就是在bash中使用BASH_REMATCH
而不是awk
或sed
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 :(得分: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
答案 2 :(得分: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
说明:
拆分为两部分,直接处理输入文件,无需循环。
提示:当需要在sed中处理单个配额时使用双重配额。
/driving to/ s/.*'([^']+)'.*:(.*)>/\2\n\t\1/p
用于获取找到汽车的内容
/no car could be found/ s/.*'([^']+)'.*/ Not Found\n\t\1/p
曾用过没有找到汽车的内容。