Extract Bluetooth MAC Address: hcitool dev
Solution 1
For you purpose is quite enough grep
hcitool dev | grep -o "[[:xdigit:]:]\{11,17\}"
-o
outputs just finded patten
[[:xdigit:]:]
mean all hexadecimal digits plus :
char
{11,17}
the set of chars should be neither less then 11 no more 17 in length
Solution 2
try
awk 'NR>1 { print $2 } ' /home/pi/mario/BT.txt
where
NR>1
means skip first row. (NR: Number of record)
Solution 3
hcitool dev | awk '$0=$2'
With awk
and many other languages, an assignment can be used as a conditional. The value assigned is then interpreted as a boolean value (integer zero, or an empty string, is "false").
In this case, the expression $0 = $2
will be "true" is there's anything in the second column. Regardless of whether there is or not, the contents of the line, $0
, will be replaced by this value.
In awk
, when a condition or pattern does not have a corresponding action block ({ ... }
) then the default action is to output the current line, as if the action had been { print $0 }
or just { print }
.
This has the effect of printing the second white-space-delimited column in the input data, but only for the lines where there is actually something in the second column.
Solution 4
cut
in:
hcitool dev | cut -sf3
out:
xx:xx:xx:xx:xx:xx
Related videos on Youtube
mario
Updated on September 18, 2022Comments
-
mario over 1 year
I have to extract from the command
hcitool dev
only the MAC address of the bluetooth dongle.Output of
hcitool dev
is:Devices: hci0 xx:xx:xx:xx:xx:xx
I write this output to a file and try to get the info with
awk
:hcitool dev > /home/pi/mario/BT.txt awk ' { print $2 } ' /home/pi/mario/BT.txt
The output also contains the first row which is an empty cell:
xx:xx:xx:xx:xx:xx
How can I put off the first cell?
-
Sreeraj over 9 yearsCan you paste a part of the contents in /home/pi/mario/BT.txt before applying
awk
? -
mario over 9 yearsThe content is equal to the output i posted before Devices: hci0 xx:xx:xx:xx:xx:xx
-
-
Costas over 9 yearsIn the case better use
$2 != ""
because in the original first row there are first field ($1) =Devices:
-
Pandya over 9 yearsThe question seems asking help for
awk
solution. But here you are usinggrep
! -
Costas over 9 years@Pandya for me it isn't look so.
-
mario over 9 yearsThanks @Costas, this is also cleaner without the need to use a file
-
voices over 8 years@mario To be fair; I think this is the real answer to your question.
-
Kusalananda over 6 years
-
terdon over 6 years@tjt263 he had an observation and a suggestion that would help you make your answer better. That's known as being helpful, not a pest. Please be careful of your tone in future.
-
voices over 6 years@terdon you're wrong.
-
terdon over 6 yearsOK. Let's try this one more time. We expect answers here to be explanatory and clear. You have posted two answers, neither of which explains what they do or how they work. That is considered a bad answer here which is why you are getting downvoted. You can now choose to follow the advice of the helpful user who took the time to link you to the help center page which explains how to write good answers, or you can ignore and/or insult the people who try to help you. Your choice.