sed
stream editor. Sed loads lines from the input into the pattern space, selectively searches and replaces text in the pattern space using regular expression syntax, then writes the pattern space to standard output.
Syntax
sed [options] file
- -i
write the transformed result directly back into the source file
- -e
allow multiple match commands to be executed at once
the s substitution command
s/pattern/replacement/ converts the 1st string matching “pattern” on each line to “replacement”
s/pattern/replacement/2 converts the 2nd string matching “pattern” on each line to “replacement”
s/pattern/replacement/g converts every string matching “pattern” on each line to “replacement”
s/pattern/replacement/2g converts every string matching “pattern” on each line, starting from the 2nd match, to “replacement”
3s/pattern/replacement/g converts every string matching “pattern” on line 3 to “replacement”
3,6s/pattern/replacement/g converts every string matching “pattern” on lines 3 through 6 to “replacement”
a semicolon ; can be used to separate multiple match commands to execute several at once
1,3s/pattern/replacement/g; 3,$s/pattern/replacement/g converts every string matching “pattern” on lines 1 through 3 to “replacement”, and every string matching “pattern” from line 3 onward to “replacement”
Example
sed -i “s/spacename/namespace/” .python_history