?
Solved

regex help

Posted on 2011-03-06
9
Medium Priority
?
436 Views
Last Modified: 2012-05-11
Hello,
For a report I need to count every rererrer in an apache log that begin with "http://"
All others I need to account for as "N/A".
I am trying to use search/replace with no luck.
Anyone have any ideas?

Here is a snippet of the log:
---------------------------------------------------------------
-
http://blizzard.ist.una.edu/~ppickett/project/research.html
http://blizzard.ist.una.edu/~jhperez/a4/index.html
-
-
-
http://myuno.una.edu/webapps/blackboard/content/courseMenu.jsp?course_id=_147588_1&mini=Y
-
http://blizzard.ist.una.edu/~lschaller/a5/
-
http://blizzard.ist.una.edu/~jdfitzpatrick/a4/otherpage.html
http://blizzard.ist.una.edu/~handersen/a5/index.html
-
 -----------------------------------------------------------------------------

The report will list the most hits, I need the top hits to display "N'A"

 Hits  %-age Resource
 -----  -----   -----
    56  55.45       -
     1   0.99 http://blizzard.ist.una.edu/1300-1-xhtml/
     1   0.99 http://blizzard.ist.una.edu/1300-2-css/
     1   0.99 http://blizzard.ist.una.edu/images/style.css
     1   0.99 http://blizzard.ist.una.edu/~adubey/project/al

Here is my attempt:
my $string7 = join('',@ref);
foreach $string7 (@ref)
{
   #$string =~ s/^http:\/\/./N\/A/mg;
   $string =~ s/-/N\/A/mg;
   print "$string7\n";
   
   $refer{$string7}++;
   $ref_tot++;
}

Open in new window

0
Comment
Question by:fac66
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 
LVL 10

Expert Comment

by:APNFSSC
ID: 35048905
Line 5 has $string and not $string7
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35048916

s/^http\:.*$/N\/A/ig;

Open in new window

0
 
LVL 16

Accepted Solution

by:
sjklein42 earned 2000 total points
ID: 35048955
Sorry, this is what I was trying to post.  It got away from me.
This replaces your line 5.  It should replace non- http:// lines with N/A.
Is this what you're trying to do?

if ( ! ( $string =~ /^http\:\/\// ) ) { $string = 'N/A'; }

Open in new window

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:APNFSSC
ID: 35048967
I think that the code is fine except for the typo?

So long as the sample provided is a true representation of the actual data
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35049012
APNFSSC - I think you're right.
0
 

Author Comment

by:fac66
ID: 35049154
Sweet.. that worked!
Thank you

Hits  %-age Resource
 -----  -----   -----
    56  55.45     N/A
0
 
LVL 10

Expert Comment

by:APNFSSC
ID: 35049452
fac66: did you try my solution above for your first issue?
0
 
LVL 16

Expert Comment

by:sjklein42
ID: 35049694
Try this:

while ( <> )
{
    if ( /^http\:\/\/([^\/]+)/ )
    {
        $refCount{$1}++;
    }
}

foreach $domain (keys(%refCount))
{
    push @lines, sprintf("%6d", $refCount{$domain}) . "\t" . $domain . "\n";
}

print sort {b <=> a} @lines;

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question