2

I'm working on a script to wake different LCs on my LAN. MAC-Adresses are hard to memorize so I made myself a list that contains information about the respective PCs. Right now the list looks like:

dragoner xxx.xxx.xxx.xxx xx:xx:xx:xx:xx:xx
theseus yyy.yyy.yyy.yyy yy:yy:yy:yy:yy:yy
...

That means, that new entries will always have to be in the form: netbios-name ipv4 mac. The advantage is, that such a list can easily be queried by grep and awk.

I'd rather like to have a list like that:

netbios=dragoner ip4=xxx.xxx.xxx.xxx mac=xx:xx:xx:xx:xx:xx
netbios=theseus mac=yy:yy:yy:yy:yy:yy ip4=yyy.yyy.yyy.yyy
...

In that manner new information could be added more easily and row wouldn't have to have a certain number of columns... but I'm failing to query it via awk.

2 Answers 2

3

In either list format you should be able to just search it. I'm not sure what you're trying to do past simple lookups but thanks to their relative formats, MAC addresses are always going to contain :, IPs . and netbios should be clean of both.

What doesn't grep '1.2.3.4' mylist do?

And you could be really bashy about this and do something like:

source <(grep '1.2.3.4' mylist)
echo "$netbios $ip"
1
  • That does the trick quite elegant.
    – Markus
    Mar 28, 2014 at 13:19
2
awk '{
    delete fields
    for (i=1; i<=NF; i++) {
        split($i, f, /=/)
        fields[f[1]] = f[2]
    }
    # do something with fields["ipv4"], fields["mac"], fields["netbios"]
}' filename

Or, don't use awk, use bash

while IFS= read -r line; do
    eval "declare $line"
    # do something with $ipv4, $mac, $netbios
done < filename

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .