Bash 使用shell脚本解析日志值

Bash 使用shell脚本解析日志值,bash,shell,sed,Bash,Shell,Sed,我正在尝试编写一个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> <Wh

我正在尝试编写一个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'>
诚然,我不太擅长编写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
i=0
对于“${grep[@]}”中的行
做

loc[i]=`sed-e“s/*\:\(.*\)>/\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
kent$f类
肯特$awk-F“(“|开车到:|>)”{printf”%s\n\t%s\n\n,NF==5?$4:“未找到”,$2;下一个}F
加拿大
系统生成。可变字符串1111
墨西哥
系统生成。可变字符串2222
找不到
系统生成。可变字符串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
kent$f类
肯特$awk-F“(“|开车到:|>)”{printf”%s\n\t%s\n\n,NF==5?$4:“未找到”,$2;下一个}F
加拿大
系统生成。可变字符串1111
墨西哥
系统生成。可变字符串2222
找不到
系统生成。可变字符串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
kent$f类
肯特$awk-F“(“|开车到:|>)”{printf”%s\n\t%s\n\n,NF==5?$4:“未找到”,$2;下一个}F
加拿大
系统生成。可变字符串1111
墨西哥
系统生成。可变字符串2222
找不到
系统生成。可变字符串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
kent$f类
肯特$awk-F“(“|开车到:|>)”{printf”%s\n\t%s\n\n,NF==5?$4:“未找到”,$2;下一个}F
加拿大
系统生成。可变字符串1111
墨西哥
系统生成。可变字符串2222
找不到
系统生成。可变字符串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
用于获取找到汽车的内容
/找不到汽车/s/*'([^']+).*/找不到\n\t\1/p
用于获取找不到汽车的内容。

使用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
用于获取找到汽车的内容
/找不到汽车/s/*'([^']+).*/找不到\n\t\1/p
用于获取找不到汽车的内容。

使用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
用于获取找到汽车的内容
/找不到汽车/s/*'([^']+).*/找不到\n\t\1/p
用于获取找不到汽车的内容。

使用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 Not found/s/*'([^']+).*/Not found\n\t\1/p
用于获取未找到car的内容。

另一种可能是在BASH中使用
BASH\u 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"

另一种可能是在BASH中使用
BASH\u重新匹配
,而不是
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"

另一种可能是在BASH中使用
BASH\u重新匹配
,而不是
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"

另一种可能是在BASH中使用
BASH\u重新匹配
,而不是
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"