Globally Replacing Pattern Matches

Globally Replacing Pattern Matches

===> cat du.examples

echo "$ `tput smso` du /u/students/* | pg `tput rmso`\c"

read line

du /u/students/* | pg

echo "$ `tput smso` du -s /u/students/* | pg `tput rmso`\c"

read line

du -s /u/students/*

===> sed '1,$s:`tput [rs]mso`::g' du.examples

echo "$ du /u/students/* | pg \c"

read line

du /u/students/* | pg

echo "$ du -s /u/students/* | pg \c"

read line

du -s /u/students/*

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

Notes:

The example above demonstrates how one can quickly remove the two commands tput smso or tput rmso; which enables and disables standout mode for a text screen session. The scenario for this is that the system you are currently doesn’t include the tput(1) command.

In defining the expression we want to:

In the replacement string includes nothing to replace. Thus tput smso or tput rmso are being removed wherever they exist. Notice the g character at the end of the last character. This is necessary to perform the required global substitution. Otherwise, the first occurrence will be substituted for only.

Notice this time we’re using the colon (:) character to delimit the expression and replacement string. The thing to remember is that the character immediately following the s (substitute directive) designates the delimiter.