14

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?

lgeorget
  • 13,656
  • 2
  • 41
  • 63
mario
  • 143
  • 1
  • 1
  • 4

5 Answers5

10

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

Costas
  • 14,806
  • 20
  • 36
4

try

 awk 'NR>1 { print $2 } ' /home/pi/mario/BT.txt

where

  • NR>1 means skip first row. (NR: Number of record)
Archemar
  • 31,183
  • 18
  • 69
  • 104
2
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.

Kusalananda
  • 320,670
  • 36
  • 633
  • 936
1

cut


in:

hcitool dev | cut -sf3

out:

xx:xx:xx:xx:xx:xx
voices
  • 1,252
  • 3
  • 15
  • 30
-2

awk


in:

hcitool dev | awk '/hci0/ {print $2}'

out:

xx:xx:xx:xx:xx:xx
voices
  • 1,252
  • 3
  • 15
  • 30
  • 2
    [Brevity is acceptable, but fuller explanations are better](https://unix.stackexchange.com/help/how-to-answer). – Kusalananda Oct 11 '17 at 12:24
  • @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. – terdon Oct 12 '17 at 14:09
  • @terdon you're wrong. – voices Oct 12 '17 at 15:25
  • 1
    OK. 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. – terdon Oct 12 '17 at 15:42