Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have a very large log file in which I need to count the occurrences of all the variations of a particular string; that is:

There are a large number of file IDs that appear in the format AA000####. I have to find out what the top five or ten IDs are in this file (which ones appear the most times).

I figure this can be done with select-string and regular expressions?

share|improve this question
    
Is the log file delimited in some way (tab, csv, etc.)? –  Scott Warren Dec 8 '11 at 18:18
    
Nope. The part I'm trying to parse out is in the middle of very long URLs as a parameter (&Title=AA000####). –  Doug Chase Dec 8 '11 at 20:48
add comment

2 Answers

up vote 3 down vote accepted

If you want to break out just the title portion (which I'm guessing you do) and not group based on the whole URL (which could contain information specific to that visit) you need to get the value of the title parameter like so:

get-content "test.txt" | % {if($_ -match 'title=([^\&]+)') {$($Matches[1])}} | group | sort -desc Count
share|improve this answer
    
This worked GREAT. Thanks!! I learned something today :) –  Doug Chase Dec 9 '11 at 23:26
add comment

This is off the top of my head but you should be able to do this with a one-liner.

You can either shove it in a variable and get the length of that variable like so:

$count = get-content .\test.txt | select-string -pattern "AA000"
$count.length

Or our can just do it all inline by using parens:

(get-content .\test.txt | select-string -pattern "AA000").length

You can do you top count with the group-object cmdlet.

get-content .\test.txt | group-object | export-csv out.csv

That is pretty ugly right now, but you should be able to go from there

share|improve this answer
    
I need to count how many times a given AA000#### appears in the file. For example, how many times was item PX0001582 requested? What I'm really interested in is which items were requested the most times. –  Doug Chase Dec 8 '11 at 20:50
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.