Home » Questions » Computers [ Ask a new question ]

How can I count commands in a log file?

How can I count commands in a log file?

I have hundreds of log files in a single directory, and I want an automated way to browse through the log files to count the number of times each command is used.

Asked by: Guest | Views: 291
Total answers/comments: 1
Guest [Entry]

"With Windows PowerShell you could do something along the lines of:

Get-Content *.log |
Where-Object { $_.StartsWith('Command:') } |
Group-Object {
$null = $_ -match '^Command: (\w+)';
$Matches[1]
} |
Select-Object Name,Count

For my test file this yields an output like

Name Count
---- -----
foo 2
bar 2
baz 1

Above code simply reads the log files line by line, pushing each line through the pipeline, it then filters the lines to only use those that start with “Command:”, indicating a command to follow. Then those lines are grouped into the individual commands. This is done by the regular expression

^Command: (\w+)

which matches the string “Command:” at the start of the line, followed by one or more word characters. This assumes the command name follows the colon and space immediately; adjust the regex accordingly if this is not the case. The command name is captured in a capture group which is used for grouping. After that only the name and frequency of the commands are selected.

The $null = part for the match is to suppress the output of the -match operator which would return always True here. We don't want to group by True bar but only by bar.

ETA: Depending on how exactly your input looks, you might want to tweak things a bit.

Allow empty commands:

^Command: (\w*)

Allow arbitrary non-space characters in command names (and empty commands):

^Command: ([^ ]*)"