one liner to search a file

Discussion in 'Perl Misc' started by h3r0, Jun 9, 2005.

  1. h3r0

    h3r0 Guest

    hi

    i need to search a file which contains the data below and extract the
    machine name only in this case 'e215c03' and overwrite the file.

    many thanks for your help.


    Name: e215c03.internal.co.uk
    Address: 10.40.10.15
     
    h3r0, Jun 9, 2005
    #1
    1. Advertising

  2. h3r0

    Ian Wilson Guest

    h3r0 wrote:
    > hi
    >
    > i need to search a file which contains the data below and extract the
    > machine name only in this case 'e215c03' and overwrite the file.
    >
    > many thanks for your help.
    >
    >
    > Name: e215c03.internal.co.uk
    > Address: 10.40.10.15
    >



    perl -n -i -e '/^Name:\s+([^\.]+)/ && print $1' filename
     
    Ian Wilson, Jun 9, 2005
    #2
    1. Advertising

  3. h3r0

    h3r0 Guest

    it wanted a backup file name, works great your a star! :)
    thanks for your fast responses.

    Ian Wilson wrote:
    > h3r0 wrote:
    > > hi
    > >
    > > i need to search a file which contains the data below and extract the
    > > machine name only in this case 'e215c03' and overwrite the file.
    > >
    > > many thanks for your help.
    > >
    > >
    > > Name: e215c03.internal.co.uk
    > > Address: 10.40.10.15
    > >

    >
    >
    > perl -n -i -e '/^Name:\s+([^\.]+)/ && print $1' filename
     
    h3r0, Jun 9, 2005
    #3
  4. h3r0

    Guest

    h3r0 <> wrote:
    > it wanted a backup file name, works great your a star! :)
    > thanks for your fast responses.
    >
    > Ian Wilson wrote:
    >> h3r0 wrote:
    >> > hi
    >> >
    >> > i need to search a file which contains the data below and extract the
    >> > machine name only in this case 'e215c03' and overwrite the file.
    >> >
    >> > many thanks for your help.
    >> >
    >> >
    >> > Name: e215c03.internal.co.uk
    >> > Address: 10.40.10.15
    >> >

    >>
    >>
    >> perl -n -i -e '/^Name:\s+([^\.]+)/ && print $1' filename

    >
     
    , Jun 9, 2005
    #4
  5. h3r0

    Guest

    Ian Wilson <> wrote:
    > h3r0 wrote:
    >> i need to search a file which contains the data below and extract the
    >> machine name only in this case 'e215c03' and overwrite the file.


    >> Name: e215c03.internal.co.uk
    >> Address: 10.40.10.15


    > perl -n -i -e '/^Name:\s+([^\.]+)/ && print $1' filename

    ^
    |
    What purpose does the backslash serve?

    Axel
     
    , Jun 9, 2005
    #5
  6. h3r0

    MrPenguin Guest

    wrote:
    > Ian Wilson <> wrote:
    >
    >>h3r0 wrote:
    >>
    >>>i need to search a file which contains the data below and extract the
    >>>machine name only in this case 'e215c03' and overwrite the file.

    >
    >
    >>>Name: e215c03.internal.co.uk
    >>>Address: 10.40.10.15

    >
    >
    >
    >>perl -n -i -e '/^Name:\s+([^\.]+)/ && print $1' filename

    >
    > ^
    > |
    > What purpose does the backslash serve?
    >
    > Axel


    to escape the dot. If not the dot is a regex symbol.
     
    MrPenguin, Jun 12, 2005
    #6
  7. h3r0

    Arne Ruhnau Guest

    MrPenguin wrote:
    > wrote:
    >
    >> Ian Wilson <> wrote:
    >>> perl -n -i -e '/^Name:\s+([^\.]+)/ && print $1' filename

    >> ^ |
    >> What purpose does the backslash serve?

    >
    > to escape the dot. If not the dot is a regex symbol.


    No, it is inside a character class and thus an ordinary dot.<- like this

    Arne Ruhnau
     
    Arne Ruhnau, Jun 12, 2005
    #7
  8. h3r0

    Rick Measham Guest

    >>> perl -n -i -e '/^Name:\s+([^\.]+)/ && print $1' filename

    >> What purpose does the backslash serve?


    Its an indication that we really do mean a dot here and not an
    'any-character'. Inside a character class, a dot is a dot. Anywhere else
    and it has a special meaning. Now when it's preceeded by a backslash in
    a character class it takes on no special meaning. The backslash is (from
    an execution POV) redundant. However when someone else comes along and
    reads your code later, they see that you really *did* mean a dot because
    you backslashed it.

    It's for the programmer, not the interpreter.

    Cheers!
    Rick Measham
     
    Rick Measham, Jun 13, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Giles Bowkett

    nifty one-liner to search ObjectSpace?

    Giles Bowkett, Mar 6, 2007, in forum: Ruby
    Replies:
    12
    Views:
    254
    Robert Klemme
    Mar 6, 2007
  2. David Stanford

    File.delete one-liner

    David Stanford, Feb 3, 2009, in forum: Ruby
    Replies:
    8
    Views:
    132
    Joel VanderWerf
    Feb 5, 2009
  3. Larry
    Replies:
    1
    Views:
    121
    Martien Verbruggen
    Feb 3, 2005
  4. h3r0

    one liner to search a file

    h3r0, Jun 9, 2005, in forum: Perl Misc
    Replies:
    1
    Views:
    88
    optional
    Jun 9, 2005
  5. cibalo
    Replies:
    2
    Views:
    455
    J├╝rgen Exner
    Jan 14, 2012
Loading...

Share This Page