SED - Stream editor
sed 's/search/replace/flag'
SED picks up each line, strips the end of line char before processing.
g -> global
I -> case invariant
w -> save file. Needs file name
p -> print the modified line
d -> Delete the line
!p -> inverse print
!d -> inverse delete
SED uses Basic Regular Expression (BRE). The following special chars needs to be escaped to make them literal strings.
$ . * / [ \ ] ^
In replacement string "&" also needs to be escaped.
SED has a feature to work on the occurrence of the pattern instead of first or global.
E.g.: Replace only the second "hello" with "hi"
echo "hello hello hello" | sed 's/hello/hi/2'
Output: hello hi hello
To filter the lines before performing an operation, we can use the '//' before 's///'
E.g.: Replace the word "v1" with "v2" in lines that start with "#"
sed '/^#/ s/v1/v2/g'
sed '\,^#, s/v1/v2/g'
Note: If a expression start with a '\' the next character is the delimiter
sed '\_/usr/local/bin_ s_/usr/local_/common/all_'
Specify a range in the file to operate on.
Line number based: sed '1,100 s/search/replace/'
Pattern based : sed '/start/,/end/ s/search/replace/'
Mixed : sed '1,/stop/ s/search/replace'
Invert range : sed '/start/,/end/ !{s/search/replace/}'
Append line after the line containing pattern
echo "abc abc abc" | sed '/abc/ a def'
Output:
abc abc abc
def
Insert a line before the line containing pattern
echo "abc abc abc" | sed '/abc/ i def'
Output:
def
abc abc abc
Change any line containing pattern
echo "abc abc abc" | sed '/abc/ c def'
Output:
def
1. SED needs the regex metachars to be escaped i.e. for grouping \(..\) needs to be used
2. SED grouped content is held in \1, \2 etc variables
3. SED -i switch can be used to edit file in-line