My google-fu's failing me again. The information is (probably) out there, but I can't find it. I know UNIX like the back of my hand, use cygwin etc. however with the increased availability of Powershell on servers, and (on production servers at least) the difficulty at getting cygwin in place, I'm attempting to pick up Powershell. If nothing else, it's another weapon in my arsenal.
Essentially, I'm looking for the Powershell equivalent of the awk command:
awk '$9 == "503" { print $0 }' < access_log
For those that don't know awk, this is basically comparing field 9 of the input file, and then executing the block (this is an apache access log, so it returns me all lines from access_log where the HTTP status code returned is 503). Awk handles the split of the file into fields based on whitespace automagically; $0 is the entire line (unadultered), with individual fields going into $1, $2, ... [etc].
I know I can use split like this:
cat access_log | %{ $_.split() }
which splits the incoming lines into an array, but I can't work out from here how to use select-object
or where-object
to select (and output) whole lines based on a given field.
The alternative is select-string
but I can't seem to see any way to pass in an expression along the lines of %{ $_.split()[8] -eq "503" }
. (I note powershell is zero-based, hence looking at field 8).
I'm not sure if I'm missing something obvious here, and I've not found the right google-fu to give me the info (so wouldn't be suprised if this is a dupe somewhere).
Cheers for any help :-)