6

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?

  • Can you paste a part of the contents in /home/pi/mario/BT.txt before applying awk? – Citylight Oct 31 '14 at 8:50
  • The content is equal to the output i posted before Devices: hci0 xx:xx:xx:xx:xx:xx – mario Oct 31 '14 at 10:26
4

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

| improve this answer | |
  • 1
    The question seems asking help for awk solution. But here you are using grep! – Pandya Oct 31 '14 at 9:20
  • @Pandya for me it isn't look so. – Costas Oct 31 '14 at 9:24
  • Thanks @Costas, this is also cleaner without the need to use a file – mario Oct 31 '14 at 10:29
4

try

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

where

  • NR>1 means skip first row. (NR: Number of record)
| improve this answer | |
  • In the case better use $2 != "" because in the original first row there are first field ($1) = Devices: – Costas Oct 31 '14 at 9:01
  • @mario To be fair; I think this is the real answer to your question. – voices Jan 20 '16 at 12:20
1

cut


in:

hcitool dev | cut -sf3

out:

xx:xx:xx:xx:xx:xx
| improve this answer | |
  • 1
    Why 3?  It looks to me like the desired output is the 2nd field. – Scott Oct 11 '17 at 16:36
1
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.

| improve this answer | |
-2

awk


in:

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

out:

xx:xx:xx:xx:xx:xx
| improve this answer | |
  • 2
  • @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

Not the answer you're looking for? Browse other questions tagged or ask your own question.