Lesson 9
Combine sed with Shell Pipelines
Objective: Filter command output and pass transformed text to another utility.
A pipe connects one command’s standard output to the next command’s standard input. Sed can occupy the middle of a pipeline because it reads standard input when no file operand is supplied. Standard error is separate unless you explicitly redirect it.
ps -ef | sed -n '/[k]sh/p'This is a text filter for process-list li nes containing ksh. The bracket expression helps avoid matching the filter’s own literal pattern, but this is still not a reliable test of executable identity: other command arguments may contain that text, and process listings change while you inspect them. Do not use this example to choose processes for termination.
Normalize leading whitespace
ps -ef | sed 's/^[[:blank:]]*//' | LC_ALL=C sort -rSed removes zero or more leading spaces or tabs. The final command sorts text in reverse byte-oriented order under the C locale; it does not sort by CPU use or by numeric process identifier. The header is ordinary text too and may move.
Use predictable input while learning
printf '%s\n' ' beta' ' alpha' 'gamma' | sed 's/^[[:blank:]]*//' | LC_ALL=C sort -rThe output is gamma, beta, then alpha on separate lines. This fixture makes the effect of each stage easy to inspect. Run each stage separately before joining a longer pipeline.
Check failures as well as text
A pipeline’s status commonly reflects its final command. In Bash, set -o pipefail makes failures in earlier stages contribute to the pipeline status; shell support and exact operational choices must be checked for the script’s target shell. A valid empty selection is not necessarily an error, so also validate expected record counts or required output.
Keep pipelines readable. Prefer explicit formats from the producing command over parsing a human-oriented display when automation depends on stable fields. Use awk for field calculations and proper parsers for structured formats rather than growing a fragile collection of substitutions.