Reply
Thread Tools
Posts: 526 | Thanked: 99 times | Joined on Sep 2009
#1
Hi All,

I've recently starting playing around with running code in the x-term and trying to create .sh files to run scripts.

I'm working on a new script and I'm a little stuck and unsure of whats happening.

lets say I have this code:

a=`hcitool scan --class | awk '/BD Address/ {print substr($3,0,8)}' | grep '00:BD:3A'`;

I'm a little unsure why the $3 field is used in the substring. I know that it's pointing to the third field but I would have though it needed the first field?

Ok so that is just for my understand, the next part is that lets say I find the code 00:BD:3A from the scan output, I then want to return the name which is the /Device name/ section of the output.
How do I this, without output the text to "table" and the filter the table buy the codes I find.

Or perhaps I am going about this the wrong way.

any help will be awesome.

P.S the output of: hcitool scan --class looks like this:

Scanning ...


BD Address: 00:00:00:00:00:00 (mode x, clkoffset 0xblah)
Device Name: blah bolah
Device class: Computer, patop (0xbalh)
__________________
Christopher Stobbs

My Blog
 
Posts: 156 | Thanked: 90 times | Joined on Jan 2010
#2
You want to get the BD address and device name?

hcitool scan --class | awk '/BD/ {print $3}'

output: 00:00:00:00:00:00


hcitool scan --class | awk -F: '$1 == "Device Name" {print $2}'

output: blah bolah

EDIT: If the space in front of the name bothers you, you can easily do it away with:

hcitool scan --class | awk -F: '$1 == "Device Name" {print $2}' | cut -c2-
 
Posts: 526 | Thanked: 99 times | Joined on Sep 2009
#3
Ok how come then if the $1 is the column tell us what we looking for example BD Address or Device name and then in the last example $2 is the name but the address is stored in $3?

Ok I see that your code will work however it doesn't include the grep (search) part, because I only want to return the names of the codes that I am looking for if that makes sense?
__________________
Christopher Stobbs

My Blog
 
Posts: 3,617 | Thanked: 2,412 times | Joined on Nov 2009 @ Cambridge, UK
#4
Originally Posted by stobbsc View Post
lets say I have this code:

a=`hcitool scan --class | awk '/BD Address/ {print substr($3,0,8)}' | grep '00:BD:3A'`;

I'm a little unsure why the $3 field is used in the substring. I know that it's pointing to the third field but I would have though it needed the first field?
That's because awk is looking at the whole line - $1 is 'BD' and $2 is 'Address:', so it's $3 you need.

Originally Posted by stobbsc View Post
Ok so that is just for my understand, the next part is that lets say I find the code 00:BD:3A from the scan output, I then want to return the name which is the /Device name/ section of the output.
How do I this, without output the text to "table" and the filter the table buy the codes I find.

Or perhaps I am going about this the wrong way.

any help will be awesome.
Personally I'd just use perl, but you can use awk as well:
Code:
a=`hcitool scan --class | awk '/BD Address/ {if (match(substr($3,0,8), "00:BD:3A") > 0) TST=1} /Device Name/ { if (TST==1) {print substr($0, 14); TST=0} }'
 
Posts: 526 | Thanked: 99 times | Joined on Sep 2009
#5
I've never used perl before or thought of using it before.

One last thing when doing the match cause you use multiple mac codes?
so for example when I grep I could have done this.
grep '00:BD:3A\|00:BD:3B\|00:21:9E'
could I do the same with the match like
match(substr($3,0,8), "00:BD:3A\|00:BD:3B\|00:21:9E"

or if you think I'm doing this in a complicated way it's better to maybe learn a new way I'm all good for learning :-)

and thanks for the explination on the $1 fields
__________________
Christopher Stobbs

My Blog
 
Posts: 3,617 | Thanked: 2,412 times | Joined on Nov 2009 @ Cambridge, UK
#6
Originally Posted by stobbsc View Post
IOne last thing when doing the match cause you use multiple mac codes?
so for example when I grep I could have done this.


could I do the same with the match like
match(substr($3,0,8), "00:BD:3A\|00:BD:3B\|00:21:9E"
You should be able to, yes - according to the documentation I've got, the match string is a regular expression. A quick test shows that it does indeed work.
 
Posts: 25 | Thanked: 13 times | Joined on Jan 2010 @ Bucharest, Romania
#7
Need GURU
Yes

Originally Posted by stobbsc View Post
a=`hcitool scan --class | awk '/BD Address/ {print substr($3,0,8)}' | grep '00:BD:3A'`;
I'm a little unsure why the $3 field is used in the substring. I know that it's pointing to the third field but I would have though it needed the first field?
Fields are separated by spaces. First field is "BD", second "Adress:", third "00:00:00:00:00"
Same output can be obtained with
hcitool scan --class | grep "BD Address" | cut -b 13-20

Ok so that is just for my understand, the next part is that lets say I find the code 00:BD:3A from the scan output, I then want to return the name which is the /Device name/ section of the output.
How do I this, without output the text to "table" and the filter the table buy the codes I find.
awk '/BD Address/ {if ("00:BD:3A"==substr($3,0,8)) getline; print $3}'

any help will be awesome.
GURU returning to meditation
 
Posts: 2,802 | Thanked: 4,491 times | Joined on Nov 2007
#8
Originally Posted by stobbsc View Post
lets say I have this code:
a=`hcitool scan --class | awk '/BD Address/ {print substr($3,0,8)}' | grep '00:BD:3A'`;


Slight optimisation: since you only care about (part of) the MAC address you can omit --class, and use sed for processing the (simpler) output in a single process:

Code:
a=`hcitool scan | sed -ne 's/^[\t]\(..:..:..\).*$/\1/' -e '/00:BD:3A/p'`
the next part is that lets say I find the code 00:BD:3A from the scan output, I then want to return the name which is the /Device name/ section of the output.
You probably want to cache the output of hcitool scan as that's the most expensive operation.

Code:
scan=`hcitool scan`
a=`echo "$scan" | sed -ne 's/^[\t]\(..:..:..\).*$/\1/' -e '/00:BD:3A/p'`
name=`echo "$scan" | awk "( substr(\$1,0,8) == \"$a\" ) { print \$2 }"`

Last edited by lma; 2010-04-09 at 10:04.
 
Posts: 2,802 | Thanked: 4,491 times | Joined on Nov 2007
#9
Perhaps a better alternative would be to grab all the relevant data in one go and assign it to variables afterwards:

Code:
data=`hcitool scan | sed -ne 's/^[\t]\(..:..:..\).*[\t]\(.*\)*$/\1 \2/' -e '/00:BD:3A/p'`
set -- $data
a=$1
name=$2
 
Posts: 526 | Thanked: 99 times | Joined on Sep 2009
#10
WOW thanks for all the help everyone :-)

Originally Posted by lma View Post
Perhaps a better alternative would be to grab all the relevant data in one go and assign it to variables afterwards:

Code:
data=`hcitool scan | sed -ne 's/^[\t]\(..:..:..\).*[\t]\(.*\)*$/\1 \2/' -e '/00:BD:3A/p'`
set -- $data
a=$1
name=$2
I think this makes the most sense. So then I can just play with the $data variable and get waht I need out of that.
I'll start playing around with code now and see what works for me :-)

Thanks again
__________________
Christopher Stobbs

My Blog
 
Reply


 
Forum Jump


All times are GMT. The time now is 00:50.