simple regex

Discussion in 'Perl Misc' started by Darkage, Aug 21, 2003.

  1. Darkage

    Darkage Guest

    I've got a file with a list of ip's in brackets, fqdn's and -'s. How do
    you simply remove the [ ]'s and the -'s from the file with a simple perl
    script. I've thought about using a if statement to take out the -'s
    lines, then maybe a double split action to get rid of the [ ] backets or
    regex substition s/// with whitespaces.

    [216.141.143.15]
    c-24-130-128-153.we.client2.attbi.com
    -
    alb92-fre1.pangeatech.com
    [216.141.143.15]
    -
    envisionpress-gw.customer.csolutions.net
    c-24-130-128-153.we.client2.attbi.com
    alb92-fre1.pangeatech.com
     
    Darkage, Aug 21, 2003
    #1
    1. Advertising

  2. Darkage

    Tore Aursand Guest

    On Fri, 22 Aug 2003 00:26:45 +1000, Darkage wrote:
    > How do you simply remove the [ ]'s and the -'s from the file with a
    > simple perl script.
    >
    > [216.141.143.15]
    > c-24-130-128-153.we.client2.attbi.com
    > -
    > alb92-fre1.pangeatech.com
    > [216.141.143.15]
    > -
    > envisionpress-gw.customer.csolutions.net
    > c-24-130-128-153.we.client2.attbi.com alb92-fre1.pangeatech.com


    cat file | perl -pe 's,\[|\]|^\-,,g'


    --
    Tore Aursand <>
     
    Tore Aursand, Aug 21, 2003
    #2
    1. Advertising

  3. Darkage

    Thens Guest

    On Thu, 21 Aug 2003 16:46:55 +0200
    Tore Aursand <> wrote:

    >
    >cat file | perl -pe 's,\[|\]|^\-,,g'


    Why not

    perl -pe 's,\[|\]|^-,,g' file

    Regards,
    Thens
    --
     
    Thens, Aug 21, 2003
    #3
  4. Darkage

    danglesocket Guest

    >>> Darkage<> 8/21/2003 10:26:45 AM >>>
    I've got a file with a list of ip's in brackets, fqdn's and -'s. How do
    you simply remove the [ ]'s and the -'s from the file with a simple perl
    script. I've thought about using a if statement to take out the -'s
    lines, then maybe a double split action to get rid of the [ ] backets or
    regex substition s/// with whitespaces.

    -If you followed through your ideas into code, you'd be right on ;-)
    -There's probably a one liner for this with 'perl -pi -e' but i couldn't get
    the two substutions in one swoop, easily,... :-(

    #!/usr/bin/perl -w

    while (<DATA>){
    s/\[(.*?)\]/$1/;
    #chomp if s/^-//; # this axe's '-' and newline
    s/^-//; # this axe's '-' and leaves newline
    print;
    }

    __DATA__
    [216.141.143.15]
    c-24-130-128-153.we.client2.attbi.com
    -
    alb92-fre1.pangeatech.com
    [216.141.143.15]
    -
    envisionpress-gw.customer.csolutions.net
    c-24-130-128-153.we.client2.attbi.com
    alb92-fre1.pangeatech.com









    __danglesocket__
     
    danglesocket, Aug 21, 2003
    #4
  5. Darkage

    David Bouman Guest

    On Thu, 21 Aug 2003 20:28:48 +0530 Thens wrote:

    > Why not
    >
    > perl -pe 's,\[|\]|^-,,g' file


    Or even:

    perl -pe 's,\[|\]|^-$/,,g' file

    --
    David.
     
    David Bouman, Aug 21, 2003
    #5
  6. Darkage

    danglesocket Guest

    >>> danglesocket<danglesocket@no_spam> 8/21/2003 11:13:53 AM >>>
    >>> Darkage<> 8/21/2003 10:26:45 AM >>>

    I've got a file with a list of ip's in brackets, fqdn's and -'s. How do
    you simply remove the [ ]'s and the -'s from the file with a simple perl
    script. I've thought about using a if statement to take out the -'s
    lines, then maybe a double split action to get rid of the [ ] backets or
    regex substition s/// with whitespaces.



    -oh yeah, what about this, with a backup.
    perl -pe 's\\[|\]|^-$/\\g' -i".bk" text.txt







    __danglesocket__
     
    danglesocket, Aug 21, 2003
    #6
  7. Thens <> wrote:

    > perl -pe 's,\[|\]|^-,,g' file


    The OP also said s?he might want to eliminate the lines with dashes:

    perl -ne "next if /^-/; s,\[|\]|,,g; print" file

    I don't use one-liners much, and was initially puzzled that 'next'
    didn't give me the expected results until I noticed that -p puts the
    print in the implicit while(){} in a continue{} block. Caveat luser.
    :)

    [Off-topic] Can domain names begin with a dash? It's my impression
    they can't, but perhaps they don't by convention. (I'm not sure which
    RFC to check)
     
    David K. Wall, Aug 21, 2003
    #7
  8. Darkage

    Sam Holden Guest

    On Thu, 21 Aug 2003 15:43:04 -0000,
    David K. Wall <> wrote:
    > Thens <> wrote:
    >
    >> perl -pe 's,\[|\]|^-,,g' file

    >
    > The OP also said s?he might want to eliminate the lines with dashes:
    >
    > perl -ne "next if /^-/; s,\[|\]|,,g; print" file
    >
    > I don't use one-liners much, and was initially puzzled that 'next'
    > didn't give me the expected results until I noticed that -p puts the
    > print in the implicit while(){} in a continue{} block. Caveat luser.
    >:)


    I'd use something like:

    perl -ne "tr/[]//d;print unless /^-/" file

    or

    perl -ne "print unless tr/[]//d,/^-/" file

    I much prefer "print unless ..." to "next if ...;print". Though using
    next does allow to avoid the s// or tr// operation, but the string
    is two characters long so it isn't going to be an expensive op.
    And using s// when tr// will do the job only adds to the complexity.

    --
    Sam Holden
     
    Sam Holden, Aug 22, 2003
    #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. =?Utf-8?B?SmViQnVzaGVsbA==?=

    Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine?

    =?Utf-8?B?SmViQnVzaGVsbA==?=, Oct 22, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    723
    =?Utf-8?B?SmViQnVzaGVsbA==?=
    Oct 22, 2005
  2. Rick Venter

    perl regex to java regex

    Rick Venter, Oct 29, 2003, in forum: Java
    Replies:
    5
    Views:
    1,649
    Ant...
    Nov 6, 2003
  3. Replies:
    2
    Views:
    611
  4. Xah Lee
    Replies:
    1
    Views:
    954
    Ilias Lazaridis
    Sep 22, 2006
  5. Replies:
    3
    Views:
    794
    Reedick, Andrew
    Jul 1, 2008
Loading...

Share This Page