grep and sed are classic examples of line filters.
What is a Line Filter?
Line filters are programs that:- Read input from standard input (stdin)
- Process data line by line
- Write output to standard output (stdout)
- Can be chained together using Unix pipes
Line filters are fundamental building blocks in Unix/Linux command-line processing and can be combined using pipes to create powerful data processing pipelines.
Basic Line Filter Example
Here’s a simple line filter that converts input text to uppercase:How It Works
Buffered Scanner
Wrapping
os.Stdin with a buffered scanner provides a convenient Scan method that advances to the next token (line by default).Reading Lines
Scan() method:
- Returns
truewhen a line is available - Returns
falseat end of file or on error - Automatically handles line ending characters
Error Handling
Usage Examples
Direct Input
File Input
Pipeline Processing
Advanced Line Filter Patterns
- Filtering
- Transformation
- Counting
- Formatting
Custom Split Functions
You can customize how the scanner splits input:Available split functions:
bufio.ScanLines(default) - split on newlinesbufio.ScanWords- split on whitespacebufio.ScanRunes- split into individual runesbufio.ScanBytes- split into individual bytes
Real-World Use Cases
Log Processing
Filter and format log files, extract error messages, or highlight specific patterns
Data Transformation
Convert data formats, normalize text, or apply transformations to each line
Text Analysis
Count words, analyze patterns, or extract statistics from text streams
Pipeline Integration
Integrate with other command-line tools in Unix pipelines
Best Practices
Use buffered I/O
Use buffered I/O
Always use
bufio.Scanner for reading line by line. It’s more efficient than reading character by character.Handle errors properly
Handle errors properly
Check
scanner.Err() after the scan loop to catch any errors that occurred during reading.Write errors to stderr
Write errors to stderr
Use
fmt.Fprintln(os.Stderr, ...) for error messages so they don’t interfere with stdout output.Exit with appropriate codes
Exit with appropriate codes
Use
os.Exit(1) for errors and os.Exit(0) (or return from main) for success.Keep filters simple
Keep filters simple
Each filter should do one thing well. Combine multiple simple filters for complex operations.
Consider performance
Consider performance
For very large files, consider processing in chunks or using goroutines for parallel processing.
Testing Line Filters
Related Topics
Reading Files
Learn more about file reading in Go
Writing Files
Learn how to write data to files