解析或过滤一些文本输出

Parsing or filtering some text output

我有一个输出(来自一个众所周知的 btmon 工具),如下例所示,我需要解析它并通过以下方式从中获取一些信息:如果 UUID 出现在 HCI event: 等于 32f9169f-4feb-4883-ade6-1f0127018db3 然后取 Address:RSSI: 字段的值并将它们放在一起并换行。例如,

A0:E6:F8:48:EF:78 B7

A0:E6:F8:48:F1:AF B7

这里是我需要按照描述的方式解析的流或文件:

> HCI Event: LE Meta Event (0x3e) plen 43                       [hci1] 0.775003
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
        Address type: Random (0x01)
        Address: 27:40:9B:C3:23:7D (Non-Resolvable)
        Data length: 31
        Company: Microsoft (6)
          Data: 010920005a6710df0df2265a4f2c5529c9cf62dfe7427b9f31f2ae
        RSSI: -87 dBm (0xa9)
> HCI Event: LE Meta Event (0x3e) plen 43                       [hci1] 1.524064
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
        Address type: Public (0x00)
        Address: A0:E6:F8:48:EF:78 (Texas Instruments Inc)
        Data length: 31
        Flags: 0x05
          LE Limited Discoverable Mode
          BR/EDR Not Supported
        Company: not assigned (30816)
          Data: ef48f8e6a000
        128-bit Service UUIDs (complete): 1 entry
          32f9169f-4feb-4883-ade6-1f0127018db3
        RSSI: -73 dBm (0xb7)
> HCI Event: LE Meta Event (0x3e) plen 43                       [hci1] 2.375147
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
        Address type: Random (0x01)
        Address: 33:02:7C:4A:D6:FC (Non-Resolvable)
        Data length: 31
        Company: Microsoft (6)
          Data: 01092000de74c964f3b4a3a59fde73f2c0c29651b9532a0e88c4e4
        RSSI: -79 dBm (0xb1)
> HCI Event: LE Meta Event (0x3e) plen 43                       [hci1] 2.876845
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Non connectable undirected - ADV_NONCONN_IND (0x03)
        Address type: Public (0x00)
        Address: A0:E6:F8:48:F1:AF (Texas Instruments Inc)
        Data length: 31
        Flags: 0x05
          LE Limited Discoverable Mode
          BR/EDR Not Supported
        Company: not assigned (44896)
          Data: f148f8e6a000
        128-bit Service UUIDs (complete): 1 entry
          32f9169f-4feb-4883-ade6-1f0127018db3
        RSSI: -73 dBm (0xb7)
> HCI Event: LE Meta Event (0x3e) plen 12                       [hci1] 2.891581
      LE Advertising Report (0x02)
        Num reports: 1
        Event type: Scan response - SCAN_RSP (0x04)
        Address type: Random (0x01)
        Address: 6B:AB:CD:7F:B7:65 (Resolvable)
        Data length: 0
        RSSI: -91 dBm (0xa5)

尝试关注 awk,如果有帮助请告诉我。

awk '/HCI Event/{A=1;next} /Address:/{val=} /32f9169f-4feb-4883-ade6-1f0127018db3/ && A{print val;val=A=""}'   Input_file

现在也添加了一种非线性形式的解决方案。

awk '/HCI Event/{
                        A=1;
                        next
                }
     /Address:/ {
                        val=
                }
     /32f9169f-4feb-4883-ade6-1f0127018db3/ && A{
                                                        print val;
                                                        val=A=""
                                                }
    '    Input_file

编辑:2

awk '/HCI Event/{A=1;next} /Address:/{val=} /32f9169f-4feb-4883-ade6-1f0127018db3/{A++} /RSSI/ && A==2{print val,toupper(substr([=12=],length([=12=])-2,2));A=""}'  Input_file

awk '/HCI Event/{
                        A=1;
                        next
                }
    /Address:/  {
                        val=
                }
    /32f9169f-4feb-4883-ade6-1f0127018db3/{
                                                A++
                                          }
    /RSSI/ && A==2{
                        print val,toupper(substr([=13=],length([=13=])-2,2));
                        A=""
                  }
    '   Input_file

这个脚本应该可以完成工作:

#!/bin/bash
inputfile="path/to/file.txt"
match=false

id=32f9169f-4feb-4883-ade6-1f0127018db3

while read -r line;
do
    if  (! echo ${line} | grep -q "HCI\|Address\|RSSI\|${id}"); then
        continue
    fi
    if echo ${line} | grep -q "> HCI Event:"; then
        if ${match}; then
            echo "${address} ${rssi:3:2}"
        fi
        address=""
        rssi=""
        match=false
        continue
    fi
    if echo ${line} | grep -q "Address: ..:..:..:..:..:.."; then
        address=`echo ${line} | grep -o "..:..:..:..:..:.."`
        continue
    fi
    if echo ${line} | grep -q "RSSI: .*dBm"; then
        rssi=`echo ${line} | grep -o "(0x..)"`
        continue
    fi
    if echo ${line} | grep -q "${id}"; then
        match=true
        continue
    fi
done < "$inputfile"

if ${match}; then
    echo "${address} ${rssi:3:2}"
fi

awk 解决办法:

awk -v uid="32f9169f-4feb-4883-ade6-1f0127018db3" '/Address:/{ addr= }
     /UUIDs/ && (getline nl > 0) && nl~uid{ uuid=nl; f=1 }
     /RSSI/{ if(f) { rssi=toupper(substr(,4,2)); print addr,rssi; f=0 } }' file

输出:

A0:E6:F8:48:EF:78 B7
A0:E6:F8:48:F1:AF B7