Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions examples/app.log
Original file line number Diff line number Diff line change
@@ -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.
32 changes: 32 additions & 0 deletions examples/count_errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//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.
//
// 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)
}
31 changes: 31 additions & 0 deletions examples/filter_csv.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
5 changes: 5 additions & 0 deletions examples/servers.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
server1,UP
server2,DOWN
server3,DOWN
server4,UP
server5,DOWN