Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > simple regex

Reply
Thread Tools

simple regex

 
 
Darkage
Guest
Posts: n/a
 
      08-21-2003
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


 
Reply With Quote
 
 
 
 
Tore Aursand
Guest
Posts: n/a
 
      08-21-2003
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 <(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
Thens
Guest
Posts: n/a
 
      08-21-2003
On Thu, 21 Aug 2003 16:46:55 +0200
Tore Aursand <(E-Mail Removed)> wrote:

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


Why not

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

Regards,
Thens
--
 
Reply With Quote
 
danglesocket
Guest
Posts: n/a
 
      08-21-2003
>>> Darkage<(E-Mail Removed)> 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__

 
Reply With Quote
 
David Bouman
Guest
Posts: n/a
 
      08-21-2003
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.
 
Reply With Quote
 
danglesocket
Guest
Posts: n/a
 
      08-21-2003
>>> danglesocket<danglesocket@no_spam> 8/21/2003 11:13:53 AM >>>
>>> Darkage<(E-Mail Removed)> 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__

 
Reply With Quote
 
David K. Wall
Guest
Posts: n/a
 
      08-21-2003
Thens <(E-Mail Removed)> 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)
 
Reply With Quote
 
Sam Holden
Guest
Posts: n/a
 
      08-22-2003
On Thu, 21 Aug 2003 15:43:04 -0000,
David K. Wall <(E-Mail Removed)> wrote:
> Thens <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How make regex that means "contains regex#1 but NOT regex#2" ?? seberino@spawar.navy.mil Python 3 07-01-2008 03:06 PM
String Pattern Matching: regex and Python regex documentation Xah Lee Java 1 09-22-2006 07:11 PM
Is ASP Validator Regex Engine Same As VS2003 Find Regex Engine? =?Utf-8?B?SmViQnVzaGVsbA==?= ASP .Net 2 10-22-2005 02:43 PM
Java regex imposture re: Perl regex compatibility a_c_Attlee@yahoo.com Java 2 05-06-2005 12:16 AM
perl regex to java regex Rick Venter Java 5 11-06-2003 10:55 AM



Advertisments