Solved

regex help

Posted on 2011-03-06
9
414 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now