sed
Introduction
sed can quickly filter, clean, and write logs for QA purposes.
sed reads from stdin (standard input) line-by-line, making it powerful for live streaming logs (e.g., adb logcat, nc, logs from protocols like FIX etc).
Basic Syntax
sed [options] commands [input-file]
[options]β flags to controlsedbehavior.'command'β what transformation to apply.[file...]β input file(s) or piped input.
Common Options
-i
In-place editing
-n
Suppress automatic printing
-e
Execute multiple commands
-f
Read commands from a file
-r
Use extended regular expressions
-E
Use extended regular expressions (similar to -r)
-z
Separate lines with NUL character
-l
Specify the line length for the 'l' command
-b
Binary mode (do not strip the CR characters)
Performance Tips (For Working with Large Logs)
Combine multiple operations
Use
-nwithpfor selective outputTest without
-ifirstFor huge files, consider splitting
Optimize sed for Speed
sed for SpeedUse
-n+pβ Suppress automatic printing and explicitlyponly needed lines:
Limit scope with addresses β Reduce processing by targeting specific line ranges:
Avoid backreferences β Capture groups
\(\...\)and&replacements slow processing.
Parallel Processing
GNU Parallel +
sedβ Process chunks simultaneously:
Split & Parallelize β For non-GNU systems:
Multi-file processing β Apply changes to many files concurrently:
Memory-Efficient Patterns
Chain commands β Combine operations in a single sed call:
Prefer d over D β Line deletion (d) is faster than pattern-space manipulation (D).
Use q for early exit β Stop processing after finding a match:
Pre-Processing Tricks
Filter first with
grepβ Reduce data before complex sed operations:
sed vs Alternatives
Feature
sed
awk
perl
Speed
Fast
Medium
Slow
Complexity
Simple
Medium
Complex
In-place edit
Yes
No
Yes
Regex support
Basic
Full
Full
Conclusion
sed excels at:
Quick text substitutions
Automated file editing
Stream processing
For complex data manipulation, consider awk or perl.
π Note: In RCA/log viewing,
sedis typically used with-nandpcommands to extract, highlight, and format, but not modify files.
π§ Tip: Always prefer to run
sedwithout-iduring investigations. Stream output into a safe file or view in terminal.
sed Cheatsheet

Last updated