sed is a Unix utility that parses and transforms text, using a simple, compact programming language.
sed is line-oriented: it reads text, line by line, from an input stream or file, into an internal buffer called the pattern space. Each line read starts a cycle. To the pattern space, sed applies one or more operations which have been specified via a sed script. sed implements a programming language with about 25 commands that specify the operations on the text. When the sed script ends, sed outputs the pattern space, and reads the next line into the pattern space, starting a new cycle, unless the q (quit) command has run, in which case sed exits and not all lines are processed. Notably, the script is run on each line, unless terminated early. A sed script corresponds to the body of a loop that iterates through the lines of a stream, where the loop itself and the loop variable are implicit (provided internally by sed).
The sed script can either be specified on the command line (-e option) or read from a separate file (-f option). Commands in the sed script may take an optional address, in terms of line numbers or regular expressions. The address determines when the command is run. For example, 2d would only run the d (delete) command on the second input line (printing all lines but the second), while /^ /d would delete all lines beginning with a space. A separate special buffer, the hold space, may be used by a few sed commands to hold and accumulate text between cycles. sed's command language has only two variables (the "hold space" and the "pattern space") and GOTO-like branching functionality; nevertheless, the language is Turing-complete and esoteric sed scripts exist for games such as sokoban, arkanoid, chess, and tetris.
A main loop executes for each line of the input stream, evaluating the sed script on each line of the input. Lines of a sed script are each a pattern-action pair, indicating what pattern to match and which action to perform, which can be recast as a conditional statement. Because the main loop, working variables (pattern space and hold space), input and output streams, and default actions (copy line to pattern space, print pattern space) are implicit, it is possible to write terse one-liner programs. For example, the sed program given by: '10q' will print the first 10 lines of input, then stop.
Common args
sed -ne '/STRING/Ip' [file]
- -n : negate (remove) output not matching
- /I : insensitive case