simple regex

D

Darkage

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
 
T

Tore Aursand

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

danglesocket

Darkage<[email protected]> 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__
 
D

danglesocket

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

David K. Wall

Thens said:
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)
 
S

Sam Holden

Thens said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Reverse search for a website 2
help with regex 7
New to python NEED SIMPLE MATH CODE plz 3
Simple Regex Doubt 4
can someone help me with this baffling regex ? 4
stuck in regex 9
regex problem 7
TF-IDF 2

Members online

No members online now.

Forum statistics

Threads
474,129
Messages
2,570,770
Members
47,329
Latest member
FidelRauch

Latest Threads

Top