CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Regular Expressions:
need regex help with replace

 



StarkRavingCalm
User

Nov 7, 2013, 7:37 AM

Post #1 of 5 (10953 views)
need regex help with replace Can't Post

I have a script I am trying to run to print certain data from a MS DNS Log.
See below:

I do a simple grep and then:

Code
foreach (@matches) { 

my $line = $_;
my @array = split/\s+/, $line;
chomp $line;

print "$array[8]\t"; print "$array[15]\n";





Problem is that column 15 has the hostnames formatted as:
(3)myhost(6)somedomain(7)com

The numbers are not always the same. I want to have a regex replace (n) with a .

What is the best way to do this and the best place in the code to put it?


Thanks!


FishMonger
Veteran / Moderator

Nov 7, 2013, 8:00 AM

Post #2 of 5 (10950 views)
Re: [StarkRavingCalm] need regex help with replace [In reply to] Can't Post

You should fix your indentation. A 1 char indent is insufficient. You should use at least 3 and max 8 space indent. My preference is 4.

Why are you doing this?

Code
my $line = $_;

It's better to declare $line in the loop initialization.

You should chomp $line before the split statement.

The regex to cleanup $array[15] (very poor choice of var name) should be between the split and print statements.

Do not put multiple statements (e.g., those print statements) on a single line. Either put them on separate lines or do it as a single print statement.

Code
foreach my $line (@matches) { 

chomp $line;
my @array = split /\s+/, $line;
$array[15] =~ s/\(\d+\)/./g;
print "$array[8]\t$array[15]\n";

}


If you only need a small subset of the array elements, then use an array slice on the split statement to only grab the fields you want.

Code
    my ($fld8, $fld15) = (split /\s+/, $line)[8,15]; 
$fld15 =~ s/\(\d+\)/./g;
print "fld8\t$fld15\n";



StarkRavingCalm
User

Nov 7, 2013, 8:07 AM

Post #3 of 5 (10948 views)
Re: [FishMonger] need regex help with replace [In reply to] Can't Post

That works! Thanks!!

Is there a quick and easy way to remove the trailing . ?


The question about the my $line = $_;
Was because it was from example code.


FishMonger
Veteran / Moderator

Nov 7, 2013, 8:14 AM

Post #4 of 5 (10945 views)
Re: [StarkRavingCalm] need regex help with replace [In reply to] Can't Post


Quote
Is there a quick and easy way to remove the trailing . ?

Sure, just add another regex.


Laurent_R
Veteran / Moderator

Nov 7, 2013, 3:21 PM

Post #5 of 5 (10931 views)
Re: [StarkRavingCalm] need regex help with replace [In reply to] Can't Post


In Reply To
Is there a quick and easy way to remove the trailing . ?

Please show a data sample. We can't guess how to do it if we don't know what it looks like in the first place.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives