Ruslan Gerasimov
Ruslan Gerasimov

Reputation: 1782

Parsing or filtering some text output

I have an output (of a well known btmon tool) as in example below and I need to parse it and get some info from it in the next way: if UUID is presented in the section HCI event: and it equals to 32f9169f-4feb-4883-ade6-1f0127018db3 then take the value of Address: and RSSI: fields and put them together and make a new line. For example,

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

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

So here is a stream or file I need parse in the described way:

> 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)

Upvotes: 0

Views: 191

Answers (3)

Lukas Isselbächer
Lukas Isselbächer

Reputation: 911

This script should do the job:

#!/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

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133458

try following awk and let me know if this helps.

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

Adding a non-one liner form of solution too now.

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

EDIT:2

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

OR

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

Upvotes: 1

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

awk solution:

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

The output:

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

Upvotes: 1

Related Questions