From 0aaf8b92162623816303bf2110e0b2d86248860f Mon Sep 17 00:00:00 2001 From: shreyaabaranwal Date: Fri, 19 Jun 2026 23:57:08 +0530 Subject: [PATCH 1/2] docs: add example to count ERROR lines in a log file (#236) Signed-off-by: shreyaabaranwal --- examples/app.log | 6 ++++++ examples/count_errors.go | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 examples/app.log create mode 100644 examples/count_errors.go diff --git a/examples/app.log b/examples/app.log new file mode 100644 index 0000000..8fcb66f --- /dev/null +++ b/examples/app.log @@ -0,0 +1,6 @@ +2026-06-19 12:00:00 [INFO] Starting application... +2026-06-19 12:01:05 [WARN] Low memory warning +2026-06-19 12:02:10 [ERROR] Database connection failed +2026-06-19 12:03:15 [INFO] Retrying database connection... +2026-06-19 12:04:20 [ERROR] Retries exhausted. Could not connect to database. +2026-06-19 12:05:00 [INFO] Shutting down application. diff --git a/examples/count_errors.go b/examples/count_errors.go new file mode 100644 index 0000000..ecdfdd7 --- /dev/null +++ b/examples/count_errors.go @@ -0,0 +1,30 @@ +// This program reads a log file, filters only the lines containing "ERROR", and prints the count of those lines. +// It uses the github.com/bitfield/script library to handle the file reading, matching, and counting. +// +// Equivalent shell command: +// grep ERROR app.log | wc -l + +package main + +import ( + "fmt" + "os" + + "github.com/bitfield/script" +) + +func main() { + // Check if examples/app.log exists, otherwise fallback to app.log. + logFile := "examples/app.log" + if _, err := os.Stat(logFile); os.IsNotExist(err) { + logFile = "app.log" + } + + count, err := script.File(logFile).Match("ERROR").CountLines() + if err != nil { + fmt.Fprintf(os.Stderr, "Error reading log file: %v\n", err) + os.Exit(1) + } + + fmt.Printf("Number of ERROR lines: %d\n", count) +} From c30c52d253ff4fc3bdd0055087e8496ffb9b3c76 Mon Sep 17 00:00:00 2001 From: shreyaabaranwal Date: Sat, 20 Jun 2026 00:32:21 +0530 Subject: [PATCH 2/2] docs: add example to filter DOWN servers from a CSV (#236) Signed-off-by: shreyaabaranwal --- examples/count_errors.go | 2 ++ examples/filter_csv.go | 31 +++++++++++++++++++++++++++++++ examples/servers.csv | 5 +++++ 3 files changed, 38 insertions(+) create mode 100644 examples/filter_csv.go create mode 100644 examples/servers.csv diff --git a/examples/count_errors.go b/examples/count_errors.go index ecdfdd7..c7272e5 100644 --- a/examples/count_errors.go +++ b/examples/count_errors.go @@ -1,3 +1,5 @@ +//go:build ignore + // This program reads a log file, filters only the lines containing "ERROR", and prints the count of those lines. // It uses the github.com/bitfield/script library to handle the file reading, matching, and counting. // diff --git a/examples/filter_csv.go b/examples/filter_csv.go new file mode 100644 index 0000000..a670e39 --- /dev/null +++ b/examples/filter_csv.go @@ -0,0 +1,31 @@ +//go:build ignore + +// This program reads a CSV file containing server names and their status (comma separated), +// filters only the lines where the status is "DOWN", and prints the name of each such server (first column). +// It uses the github.com/bitfield/script library to handle the file reading, matching, replacing, and filtering. +// +// Equivalent shell command: +// grep DOWN servers.csv | cut -d, -f1 + +package main + +import ( + "fmt" + "os" + + "github.com/bitfield/script" +) + +func main() { + // Check if examples/servers.csv exists, otherwise fallback to servers.csv. + csvFile := "examples/servers.csv" + if _, err := os.Stat(csvFile); os.IsNotExist(err) { + csvFile = "servers.csv" + } + + _, err := script.File(csvFile).Match("DOWN").Replace(",", " ").Column(1).Stdout() + if err != nil { + fmt.Fprintf(os.Stderr, "Error reading CSV file: %v\n", err) + os.Exit(1) + } +} diff --git a/examples/servers.csv b/examples/servers.csv new file mode 100644 index 0000000..e7a29ef --- /dev/null +++ b/examples/servers.csv @@ -0,0 +1,5 @@ +server1,UP +server2,DOWN +server3,DOWN +server4,UP +server5,DOWN