4 ms·
Note that you can put conditions before the braces, so you can also express the example awk '{if ($(NF-2) == "200") {print $0}}' logs.txt as awk '$(N
by Pete_D 8y ago
Note that you can put conditions before the braces, so you can also express the example
awk '{if ($(NF-2) == "200") {print $0}}' logs.txt
as
awk '$(NF-2) == "200" { print $0 }' logs.txt
IMO, this style scans easier for most of the grep-ish use cases.
- kazinator 8y ago{ print $0 } is the default action if you don't specify one. Using "200" in quotes makes sense if you're looking for an exact string match; this will fail if the datum is 0200, or 200.0.
- newscracker 8y agoMy guess is that the GP's example is to get all HTTP status 200 lines from a web server log file. In that case, a precise match for "200" is fine.
- vram22 8y ago{ print $0 }, { print }, and nothing (i.e. no action given - but a pattern is given) are all equivalent, and result in printing the current line of the input (if it matches the pattern). Similarly, if no pattern is given, but an action is given, the default is to match every line (and so perform the given action on every line). Either the pattern or the action must be given, or both.
- cup-of-tea 8y agoMuch better. Awk is first a filter, then a processor. As others mentioned you don't even need a processor here. Using awk as just a filter is great and easily communicated by proper use of the language.