Results 1 to 8 of 8

Thread: Grep for multiple words and phrases

  1. #1
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    11,728
    Blog Entries
    2

    Default 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:
    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
    Leap 42.3 & 15.1 &KDE
    FYIs from the days of yore

  2. #2
    Join Date
    Jul 2008
    Location
    US - MA
    Posts
    34

    Default Re: Grep for multiple words and phrases

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

    Code:
    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.
    =============================================
    openSUSE 11.0 Intel Quad-Core @2.4ghz
    NVIDIA GeForce 7900 GTO
    4GB RAM 1TB diskspace (SATA)

    =============================================

  3. #3
    ab@novell.com NNTP User

    Default Re: Grep for multiple words and phrases

    -----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-----

  4. #4
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Grep for multiple words and phrases

    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.

  5. #5
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    27,125
    Blog Entries
    15

    Default Re: Grep for multiple words and phrases

    [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
    Code:
    grep -v ^# <your conf file> | grep -v ^$
    I'm guessing awk, sed will probably refine it more... man bash, then;

    Code:
    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


  6. #6
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    11,728
    Blog Entries
    2

    Default Re: Grep for multiple words and phrases

    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.
    Leap 42.3 & 15.1 &KDE
    FYIs from the days of yore

  7. #7
    Join Date
    Jun 2008
    Location
    Earth - Denmark
    Posts
    10,730

    Default Re: Grep for multiple words and phrases

    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

  8. #8
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    11,728
    Blog Entries
    2

    Default Re: Grep for multiple words and phrases

    Quote Originally Posted by DenverD View Post
    ideally follow-on folks looking for a similar answer should look
    there, instead of here..
    Thanks for that, but I think it should be with "Applications" because it's a question about a Linux app (grep, egrep).
    Leap 42.3 & 15.1 &KDE
    FYIs from the days of yore

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •