Tutorial by Examples

Variables inside single quotes ' don't get expanded by POSIX compatible shells, so using a shell variable in a sed substitution requires the use of double quotes " instead of single quotes ': $ var="he" $ echo "hello" | sed "s/$var/XX/" XXllo $ var="he&q...
Using escaped brackets, you can define a capturing group in a pattern that can be backreferenced in the substitution string with \1: $ echo Hello world! | sed 's/\(Hello\) world!/\1 sed/' Hello sed With multiple groups: $ echo one two three | sed 's/\(one\) \(two\) \(three\)/\3 \2 \1/' three ...
POSIX/IEEE Open Group Base Specification says: [2addr] s/BRE/replacement/flags Substitute the replacement string for instances of the BRE in the pattern space. Any character other than backslash or newline can be used instead of a slash to delimit the BRE and the replacement. Within the BRE a...
If we want to replace only the first occurrence in a line, we use sed as usual: $ cat example aaaaabbbbb aaaaaccccc aaaaaddddd $ sed 's/a/x/' example xaaaabbbbb xaaaaccccc xaaaaddddd But what if we want to replace all occurrences? We just add the g pattern flag at the end: $ sed 's/a/x/...

Page 1 of 1