Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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 Keck-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
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

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"

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

Your Answer


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.