Sed, or Stream Editor as it is commonly known, is mainly used for parsing text and transforming any text within a script. This feature of Sed is used in Shell scripts to generate flexible and dynamic codes. Knowing the common tips and tricks
can help users to optimize their codes to a great extent. One can do custom transformations of text such as substituting
one text with another based on number of occurrence, displaying specific lines
and removing spaces or blanks. One can also use tips and tricks
for miscellaneous items such as joining multiple lines
or replacing special characters
from specific lines as well.
=[Sed] Tips and tricks
Substitute "foo" with "bar" on each line
Only the 1st occurrence
sed 's / foo / bar /'
For the 3rd case only
sed 's / foo / bar / g'
Just before the last occurrence
sed 's / \ (.* \) foo \ (.* foo \) / \ 1bar \ 2 /'
Only the last case
sed 's / \ (.* \) foo / \ 1bar /'
Substitute "foo" with "bar" only lines containing "plop"
sed '/ plop / s / foo / bar / g'
Substitute "foo" with "bar" except the lines containing "plop"
sed '/ plop /! s / foo / bar / g '
Replace "Foo" or "foo" with "bar" on each line
sed 's / [Ff] oo / bar / g'
Replace "blue" or "white" or "red" with "green"
sed 's / blue \ | blank \ | red / green / g'
The 1st line (head -1)
The first 5 lines (head -5)
sed'5 q '
sed'1, 5! d '
The last line (tail -1)
sed-n '$ p'
sed '$! d "
The last 5 lines (tail -5)
sed-e: a-e '$ q, N, 6, $ D; ba'
The 2 last lines (tail -2)
sed '$! N; $! D'
Only lines matcha a reason or a regular expression
sed-n '/ pattern / p'
sed '/ regexp /! d'
Only lines that matcha not a reason or a regular expression
sed-n '/ pattern /! p'
sed '/ regexp / d'
The line preceding a pattern or a regular expression
sed-n '/ pattern / (g; 1! p;) h'
The line following a pattern or a regular expression
sed-n '/ regexp / (n, p;)'
===Space and tab==
Removing spaces and tabs
At the beginning of the line
sed 's / ^ [\ t] * / /
sed 's / ^ \ s * / /' # Using the parameter "\ s"
At end of line
sed 's / [\ t ]*$//'
At the beginning and end of line
sed 's / ^ [\ t ]*//; s / [\ t ]*$//'
Removing blank lines
All empty lines
Only those at the top
sed-nr'/./,$ /(.*)/ s \ 1 / p '#
thank you Adrien
Only those at end
sed-e: a-e '/ ^ \ n * $ / ($ d N; ba'-e ')'
Eliminate a line at regular intervals
All lines pairs
sed'1 ~ 2d '
All the odd lines
sed'2 ~ 2d '
Every n lines from the line n
sed'3 ~ 2d '# 2
All lines from line 3
Attach lines 2 by 2
sed '$! N s / \ n / /'
Attach the 3 lines by 3
sed '$! N s / \ n //;$! N s / \ n / /;'
If a line ends with a backslash (\), add the following line and replace the end of line (\ n) by a space
sed-e: a-e '/ \ \ $ / N s / \ \ \ n / /; ta'
If a line begins with an equal sign (=), add it to the previous line and replace the equal sign (=) with a space
sed-e: a-e '$! N s / \ n = / /; ta'-e 'P, D'