Solved

regex help

Posted on 2011-03-06
9
421 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
  • 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 500 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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
 

Author Comment

by:fac66
ID: 35049415
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…

860 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