githubEdit

rectangle-terminalsed

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 control sed behavior.

  • 'command' β€” what transformation to apply.

  • [file...] β€” input file(s) or piped input.

Common Options

Option
Example
Description

-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 -n with p for selective output

  • Test without -i first

  • For huge files, consider splitting

Optimize sed for Speed

  • Use -n + p – Suppress automatic printing and explicitly p only 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, sed is typically used with -n and p commands to extract, highlight, and format, but not modify files.

🧠 Tip: Always prefer to run sed without -i during investigations. Stream output into a safe file or view in terminal.

sed Cheatsheet

Last updated