Solved

regex help

Posted on 2011-03-06
9
397 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
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…
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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

14 Experts available now in Live!

Get 1:1 Help Now