Professional Web Applications Themes

grep command and EXACT host entry in /etc/hosts - Sun Solaris

Hi all! I need some help with grep|egrep|fgrep. I want the (e)(f)grep command to get 1 exact match for a host entry in /etc/hosts. Let's assume you have the following lines in /etc/vfstab ------------------------------------- 192.168.10.10 start 192.168.10.11 start1 192.168.10.12 starter I need grep to only match the word start , whether it has spaces or tabs after the word. Any help would be appreciated, Daniel...

  1. #1

    Default grep command and EXACT host entry in /etc/hosts

    Hi all!

    I need some help with grep|egrep|fgrep.

    I want the (e)(f)grep command to get 1 exact match
    for a host entry in /etc/hosts.

    Let's assume you have the following lines in /etc/vfstab
    -------------------------------------
    192.168.10.10 start
    192.168.10.11 start1
    192.168.10.12 starter

    I need grep to only match the word start , whether it has spaces or
    tabs after the word.

    Any help would be appreciated,

    Daniel
    Daniel Guest

  2. #2

    Default Re: grep command and EXACT host entry in /etc/hosts

    Daniel <com> wrote: 

    If your grep supports the -w option (not sure if all of them do),
    then this will work:

    grep -w start <filename>

    Alternatively, try:

    grep 'start[ \t]*$' <filename>

    mp.
    --
    Systems Administrator | Institute for Software Science | Univ. of Vienna
    Martin Guest

  3. #3

    Default Re: grep command and EXACT host entry in /etc/hosts


    "Daniel" <com> wrote in message
    news:google.com... 

    You mean /etc/hosts, I hope?
     

    grep -w start /etc/hosts
     

    --
    Noel R. Nihill
    UNIX® platform development
    Motorola NSS
    I *could* be arguing in my spare time.


    Noel Guest

  4. #4

    Default Re: grep command and EXACT host entry in /etc/hosts

    In article <3f82cb75$0$24956$univie.ac.at>,
    Martin Paul <univie.ac.at> writes: 
    >
    > If your grep supports the -w option (not sure if all of them do),
    > then this will work:
    >
    > grep -w start <filename>
    >
    > Alternatively, try:
    >
    > grep 'start[ \t]*$' <filename>[/ref]

    Another one:

    awk '$2=="start" {print $0}' /etc/hosts

    which would be particularly convenient if you just wanted to
    print $1 (i.e. the IP address) instead of whole line.

    Yet another one:

    getent hosts start

    although this will look in the hosts naming database, which might
    not be /etc/hosts but will be whatever your system is really using
    for name resolving. Also, getent(1M) is part of NIS+ and hence will be
    missing from some systems (will be fixed in next release of Solaris).

    --
    Andrew Gabriel
    Andrew Guest

  5. #5

    Default Re: grep command and EXACT host entry in /etc/hosts

    In article <bluifr$g5a$corp.mot.com>, Noel R. Nihill wrote: 
    >
    > You mean /etc/hosts, I hope?

    >
    > grep -w start /etc/hosts[/ref]

    Another way to do it is:

    grep '\<start\>' /etc/hosts

    \< means match the beginning of a word and \> means match the end of a
    word.
     
    >
    > --
    > Noel R. Nihill
    > UNIX® platform development
    > Motorola NSS
    > I *could* be arguing in my spare time.
    >
    >[/ref]


    --
    Jim Cochrane; com
    [When responding by email, include the term non-spam in the subject line to
    get through my spam filter.]
    Jim Guest

  6. #6

    Default Re: grep command and EXACT host entry in /etc/hosts

    Okay, thanks to all of you for your answers!

    Yes, I meant /etc/hosts.. =)


    To grow this discussion a bit.

    Let's assume you have the following host entries in /etc/hosts
    -----------------
    192.169.10.10 test
    192.168.10.11 test-node
    192.168.10.12 test123

    I get problems when trying to grab "test" WHEN

    grep '\<test\>' returns both test and test-node
    grep 'test[ \t]*$' doesnt return a match if TAB exit's after test

    BUT

    grep 'test[ \(CTRL+v then TAB]*$ works fine,even if TAB exist's,
    but can't be used in a shellscript... =(

    So, how do get the TAB's???

    TIA,

    Daniel




    How should the grep look like to be sure to


    Jim Cochrane <dimensional.com> wrote in message news:<dimensional.com>... 
    > >
    > > You mean /etc/hosts, I hope?
    > > 
    > >
    > > grep -w start /etc/hosts[/ref]
    >
    > Another way to do it is:
    >
    > grep '\<start\>' /etc/hosts
    >
    > \< means match the beginning of a word and \> means match the end of a
    > word.

    > >
    > > --
    > > Noel R. Nihill
    > > UNIX® platform development
    > > Motorola NSS
    > > I *could* be arguing in my spare time.
    > >
    > >[/ref][/ref]
    Daniel Guest

  7. #7

    Default Re: grep command and EXACT host entry in /etc/hosts

    Daniel <com> wrote: 

     
     
     
     

    That's because '\>' (end of word) is matched when you go from a letter
    (in this case the t) to a non-letter (the dash). You'd have to change
    grep's notion of a "word character" to include the dash.
     
     
     

    Why not?

    --
    Darren Dunham com
    Unix System Administrator Taos - The SysAdmin Company
    Got some Dr Pepper? San Francisco, CA bay area
    < This line left intentionally blank to confuse you. >
    Darren Guest

  8. #8

    Default Re: grep command and EXACT host entry in /etc/hosts

    In article <google.com>, Daniel wrote: 

    How about:

    egrep '[ ]test[^A-Za-z0-9-_.]|[ ]test[ ]*$' $*

    Note that all 3 [...]s with blanks inside have both a space and a tab
    character. The above will also catch lines like:

    192.169.10.10 test testalias

    (assuming that's what you want). Also, make sure any character that's
    legal in a hosts file, except space and tab, is listed in the
    "[^A-Za-z0-9-_.]" expression.
     
    >>
    >> Another way to do it is:
    >>
    >> grep '\<start\>' /etc/hosts
    >>
    >> \< means match the beginning of a word and \> means match the end of a
    >> word.
    >> [/ref][/ref]


    --
    Jim Cochrane; com
    [When responding by email, include the term non-spam in the subject line to
    get through my spam filter.]
    Jim Guest

  9. #9

    Default Re: grep command and EXACT host entry in /etc/hosts



    On Wed, 8 Oct 2003, Daniel wrote: 

    egrep '[ ]test([ ]+.*)*$'
    ^^
    \\TAB
    \SPACE

    where the two characters between the square brackets [] are a space and a
    tab, works fine for me but I must say thats it's a bit irritating that the
    grep/egrep on my sun system seems to ignore the \t and not expanding it,
    regardless if I use " or ' around the argument.

    /Johan A


    Mr. Guest

  10. #10

    Default Re: grep command and EXACT host entry in /etc/hosts

    Mr. Johan Andersson <mh.se> wrote: 

    TAB=`echo ""|awk '{print "\t"}
    # Space-Tab
    ST=" $TAB"

    egrep "[$ST]...
    Cypherpunk@nyc.rr.com Guest

  11. #11

    Default Re: grep command and EXACT host entry in /etc/hosts

    rr.com wrote: 
    >
    > TAB=`echo ""|awk '{print "\t"}[/ref]

    Yeah, something like that.

    TAB=`echo ""|awk '{print "\t"}'`
     

    Your shell script has regained its style with that edit.
    Cypherpunk@nyc.rr.com Guest

  12. #12

    Default Re: grep command and EXACT host entry in /etc/hosts

    rr.com wrote:
     
    >>
    >> TAB=`echo ""|awk '{print "\t"}[/ref]
    >
    >
    > Yeah, something like that.
    >
    > TAB=`echo ""|awk '{print "\t"}'`[/ref]

    Or, since there is always more than one way to do it, here is a
    shorter way:

    TAB=`echo | tr \\n \\t`

    A less-portable way:

    TAB=`/bin/echo '\t\c'`

    There is also this:

    TAB=`printf \\t`

    Again, that's not terribly portable.

    - Logan

    Logan Guest

Similar Threads

  1. How to fix a 'grep' command from LINUX to UNIX?
    By Nuage in forum UNIX Programming
    Replies: 11
    Last Post: January 30th, 12:12 AM
  2. Multiple IP Addresses For Same Host in /etc/hosts
    By Tennis Smith in forum Linux / Unix Administration
    Replies: 1
    Last Post: December 9th, 10:15 PM

Bookmarks

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139