Grep for multiple words and phrases

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.

Thanks

Pretty much what you are doing … if, from your example where you want (space)Bcast and Ethernet(space)(space)HW, you would do:

ifconfig|egrep "Ethernet  HW| Bcast"
eth0      Link encap:Ethernet  HWaddr 00:19:D1:81:D6:2E
          inet addr:192.168.2.119  Bcast:192.168.2.255  Mask:255.255.255.0

So put the exact phrases within double quotes like you are doing and it should work fine.

Understanding regular expressions, regexps or REs are the key to grep and other uses of REs. Here are a couple of links to get you started:

Regular expression - Wikipedia, the free encyclopedia
Regular-Expressions.info - Regex Tutorial, Examples and Reference - Regexp Patterns

Then you can tackle the options that grep offers.

As a hint, you would need something like:

grep -i ‘name +resolve +order’

  • is the once or more times operator.

If you are dealing with multiline options, it might be easier to write the program in Perl.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

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

iD8DBQFIszaR3s42bA80+9kRAp43AJ91L0yKB5j7WWJyl0Rhf9mN4kG0dwCeNefr
47Q68SdeQMmAdfgH0rx2ABs=
=Op0X
-----END PGP SIGNATURE-----

[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 :slight_smile:


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.

Thanks again.

swerdna wrote:
> I am writing some code.

maybe help is also available if posted in (or moved to) the
programming-scripting forum…

ideally follow-on folks looking for a similar answer should look
there, instead of here…


see caveat: http://tinyurl.com/6aagco
DenverD (Linux Counter 282315) via NNTP, Thunderbird 2.0.0.14, KDE
3.5.7, SUSE Linux 10.3, 2.6.22.18-0.2-default #1 SMP i686 athlon

Thanks for that, but I think it should be with “Applications” because it’s a question about a Linux app (grep, egrep).