Counting with Different Script Languages
Every day at work, I make sure to learn at least one more Powershell function, and I'm bridging my bash knowledge over. Net goal; I can do everything on a Windows server that I can do on a Linux server, if not more.
I do a huge amount of log file review, and one common thing I'll do is to take data like this:
2023-09-06 12:22:47.008 [Pickle Thread] Reticulating Splines
2023-09-06 12:22:47.019 [Apple Thread] Forecasting future misinformation
2023-09-06 12:22:48.100 [Pickle Thread] Neglecting the step children
2023-09-06 12:22:48.219 [Apple Thread] Registering pain points
2023-09-06 12:22:49.062 [Banana Thread] Organizing a Raffle
.. let's say this goes on indefinitely. I want to know how many times in my log each of my fruity threads appears. In Bash, that looks like this:
cat sample.log | sed 's/.*\[//;s/\].*//' | sort | uniq -c
2 Apple Thread
1 Banana Thread
2 Pickle Thread
In powershell, that looks like this
gc .\sample.log | Group-Object { $_ -match "\[(.*?)\]" | Out-Null; $Matches[1] }
Count Name Group
----- ---- -----
2 Pickle Thread {Pickle Thread, Pickle Thread}
2 Apple Thread {Apple Thread, Apple Thread}
1 Banana Thread {Banana Thread}