0

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)

3 Answers 3

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Could you please advice a modification for this if we need to output to different files according [hci] field so if I have [hci0] then all such instances go to file0, all [hci1] go to file1 and so on? But the main output (first version) should be also kept too. Thank you.
@RuslanGerasimov, you can create a new question for new conditions, posting the expected result for the first few files
1

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

5 Comments

this gave only A0:E6:F8:48:EF:78 A0:E6:F8:48:F1:AF without RSSI
@RuslanGerasimov, kindly check my update, EDIT2 now and let me know if this output you need.
This gave: A0:E6:F8:48:EF:78 RSSI: -73 dBm (0xb7) A0:E6:F8:48:F1:AF RSSI: -73 dBm (0xb7) a little bit more as for RSSI part. I think kee[ the edit2 anyway, also possible, even if you'd make edit3.
this EDIT2 should work now, sorry overlooked that RSSI part, check now.
Could you please advice a modification for this if we need to output to different files according [hci] field so if I have [hci0] then all such instances go to file0, all [hci1] go to file1 and so on? But the main output (first version) should be also kept too. Thank you.
1

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

8 Comments

./filter_script.sh: line 24: syntax error near unexpected token done' ./filter_script.sh: line 24: done < "$inputfile"'
@RuslanGerasimov Sorry, forgot the ; do statement. Edited the code. Runs now!
Thnak you. It gave: A0:E6:F8:48:F1:AF (0xc1) and could it be without parenthesizes? Another thing is your script works very slowly like one second per line (per address) - can this be fixed?
@RuslanGerasimov Added some speed improvements and rssi is now only showing the value.
./filter2_script.sh: line 33: test.txt: No such file or directory ./filter2_script.sh: line 37: unexpected EOF while looking for matching ``' ./filter2_script.sh: line 38: syntax error: unexpected end of file
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.