sed

sed maintains two data buffers: the active pattern space, and the auxiliary hold space. Both are initially empty.

By default, sed reads a line from the input stream, removes the newline character and places it in the pattern space, then continues to process all commands in order. Commands without addresses affect all lines. Commands with addresses affect matching lines.

When the end of the script is reached, unless the -n option is in use, the contents of the pattern space are printed out, adding back any trailing newline. Then the cycle restarts with the next input line.

Unless special commands (like ‘D’) are used, the pattern space is deleted every two cycles. The hold space, on the other hand, keeps its data between cycles (see commands ‘h’, ‘H’, ‘x’, ‘g’, ‘G’ to move data between both buffers).

Examples

Find all named files and add a new line to each before line 11

find . -name "index.html" -exec sed -i 11a\ '<link rel="icon" href="/favicon.jpg">' {} \;