Changing a character string
For changing text or correcting spelling errors, use the s (substitute)
subcommand. When you enter the subcommand, the line you are changing
becomes your current working line. To display the line after you
make the change, enter the p (print) subcommand.
- To substitute text for the first matching string on the current
working line, enter:
s/oldtext/newtext/
- To substitute text for the first matching string on a specified
line, enter:
where n is the number of the line.ns/oldtext/newtext/
- To substitute text for the first matching string on more than
one line, enter:
where a1 is the number (or address) of the first line to be changed and a2 is the number of the last line to be changed.a1,a2s/oldtext/newtext/
- To change every occurrence of a string on more than one line,
enter:
where a1 is the number of the first line to be changed and a2 is the number of the last line to be changed. g is the global operator.a1,a2s/oldtext/newtext/g
To change every occurrence of a string on one line, enter:
g is the global operator.ns/oldtext/newtext/g
- To delete a word or string, enter:
s/oldtext//