aboutsummaryrefslogtreecommitdiffstats
path: root/awk/rawk/example.rawk
diff options
context:
space:
mode:
Diffstat (limited to 'awk/rawk/example.rawk')
-rw-r--r--awk/rawk/example.rawk13
1 files changed, 7 insertions, 6 deletions
diff --git a/awk/rawk/example.rawk b/awk/rawk/example.rawk
index e5bce3c..bda56b7 100644
--- a/awk/rawk/example.rawk
+++ b/awk/rawk/example.rawk
@@ -1,8 +1,8 @@
-# Apache Log Processing Example
-# This demonstrates most rawk features with real-world data processing
+# This demonstrates most rawk features in a setting familiar to awk
# Usage: awk -f rawk.awk example.rawk | awk -f - sample.log
-# Predicate functions for log analysis
+# User defined predicate functions for log analysis
+# This shows off rawk's single line function syntax
$is_error = (status) -> status >= 400;
$is_success = (status) -> status >= 200 && status < 300;
$is_large_request = (bytes) -> bytes > 1000000; # > 1MB
@@ -10,6 +10,7 @@ $is_api_request = (url) -> index(url, "/api/") > 0;
$is_bot = (user_agent) -> index(user_agent, "bot") > 0 || index(user_agent, "crawler") > 0;
# Data parsing and transformation functions
+# These show off rawk's multi-line arrow functions
$extract_status = (request_line) -> {
split(request_line, parts, " ")
# The status code is the second part, not the third
@@ -46,7 +47,7 @@ BEGIN {
# Process each log line
{
# Parse Apache log format: IP - - [timestamp] "method url status" bytes "referer" "user-agent"
- # Use a simpler approach with multiple regex matches
+ # Note that we use a series of simpler regex matches, rather than trying to do it all at once
if (match($0, /^([0-9.]+)/)) {
ip = substr($0, RSTART, RLENGTH)
@@ -78,7 +79,7 @@ BEGIN {
# Store for analysis
request_count++
- # Real-time processing with standard library predicates
+ # Real-time processing using some standard library predicates
if (http_is_server_error(status)) {
server_error_count++
error_report = format_error_report(ip, status, url, user_agent)
@@ -147,7 +148,7 @@ END {
print "Bot Requests:", bot_count
print "Large Requests (>1MB):", large_count
- # Demonstrate functional programming features
+ # Some functional patterns at play, map, flatMap, and take.
if (api_count > 0) {
print ""
print "API Usage Analysis"