SED with Global Substitutions

SED with Global Substitutions

===> sed -n 1,$s/program/PROGRAM/p fortunes

Every PROGRAM is a part of some other program, and rarely fits.

Old PROGRAMmers never die. They just branch to a new address.

===> sed -n 1,$s/program/PROGRAM/gp fortunes

Every PROGRAM is a part of some other PROGRAM, and rarely fits.

Old PROGRAMmers never die. They just branch to a new address.

Previous slide Next slide Back to the first slide View Graphic Version

Notes:

This is a simple program demonstrating how sed performs substitutions.

In the first example, we say from lines to the end-of-file (denoted by the $ character), that a substitution is to be performed (denoted by the s preceding the first /). The substitution is defined as replace the first occurrence of program with PROGRAM working left to right.

In order to perform a global substitution, you must include the g character as part of the parameter list provided after the last slash.

The slashes are being uses as delimiters for /expression/replacement string/. However we can use different characters as a delimiter.