I am writing some code. I want to extract from “ifconfig” the device names and IP addresses. For example this expression will get me a result I want:
ifconfig | egrep "Ethernet|Bcast"
Here is the output:
ath0 Link encap:Ethernet HWaddr 00:17:9A:75:CA:A6
inet addr:192.168.1.2 Bcast:192.168.1.255 Mask:255.255.255.0
Then I got to thinking: how do I cope with phrases (in the future). For example what would be the grep command to return lines containing the phrases ‘Ethernet HW’ and ’ Bcast’ (that’s Bcast with a space in front of it)?
I need phrases because I want to grep files like smb.conf for lines containing phrases like these:
name resolve order
netbios name
map to guest
I’ve looked at ‘man grep’ and it just leaves me confused for this level of detail.
Doesn’t your existing output already have a space at the beginning? It
surely seems to based on your output. Anyway, grep doesn’t care about a
space before what it finds (at least in your case) because of how the
regex is setup. You can force no-space before/after some other pattern
but in your case it is not done so it is not enforced.
Good luck.
swerdna wrote:
> I am writing some code. I want to extract from “ifconfig” the device
> names and IP addresses. For example this expression will get me a result
> I want:
>
> Code:
> --------------------
> ifconfig | egrep “Ethernet|Bcast”
> --------------------
>
> Here is the output:
>> ath0 Link encap:Ethernet HWaddr 00:17:9A:75:CA:A6
>> inet addr:192.168.1.2 Bcast:192.168.1.255
>> Mask:255.255.255.0
>
> Then I got to thinking: how do I cope with phrases (in the future). For
> example what would be the grep command to return lines containing the
> phrases ‘Ethernet HW’ and ’ Bcast’ (that’s Bcast with a space in front
> of it)?
>
> I need phrases because I want to grep files like smb.conf for lines
> containing phrases like these:
> name resolve order
> netbios name
> map to guest
>
> I’ve looked at ‘man grep’ and it just leaves me confused for this level
> of detail.
>
> Thanks
>
>
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org
[QUOTE=swerdna]
I am writing some code. I want to extract from “ifconfig” the device
names and IP addresses. For example this expression will get me a result
I want:
Code:
ifconfig | egrep “Ethernet|Bcast”
Here is the output:
> ath0 Link encap:Ethernet HWaddr 00:17:9A:75:CA:A6
> inet addr:192.168.1.2 Bcast:192.168.1.255
> Mask:255.255.255.0
Then I got to thinking: how do I cope with phrases (in the future). For
example what would be the grep command to return lines containing the
phrases ‘Ethernet HW’ and ’ Bcast’ (that’s Bcast with a space in front
of it)?
I need phrases because I want to grep files like smb.conf for lines
containing phrases like these:
name resolve order
netbios name
map to guest
I’ve looked at ‘man grep’ and it just leaves me confused for this level
of detail.
Thanks
/QUOTE]
Hi
This should give you a list of all the active options
grep -v ^# <your conf file> | grep -v ^$
I’m guessing awk, sed will probably refine it more… man bash, then;
man bash
/regex<enter>
I use perl, so much easier
–
Cheers Malcolm °¿° (Linux Counter #276890)
openSUSE 11.0 x86 Kernel 2.6.25.11-0.1-default
up 7:15, 2 users, load average: 0.18, 0.40, 0.32
GPU GeForce 6600 TE/6200 TE - Driver Version: 173.14.12
Well thankyou all. Don’t know why I didn’t think of the “excat phrase|another phrase” version. OK got it now e.g.
john@suse110:~> cat /etc/samba/smb.conf | grep -E “name resolve order|netbios name|local master”
netbios name = suse110
name resolve order = bcast host lmhosts wins
local master = yes
What a hoot!
The other suggestions I’m tracking down as we speak.
Regarding perl and other scripts: eventually I want to make available software to others so I need it to be not dependent on them having to install scripting language/s. So I’m writing in a compilable language → independent executable file.