sed delete line match code example

Example 1: sed remove line after match

Use d option at the end of sed command string for deleting line of matches.
sed -i "" "s/matching_pattern/,+1 d" file.txt #+1 defines number of lines
# to delete after matching line

Example 2: sed delete line before match

Use d option at the end of sed command string for deleting line of matches.
tac | sed -i "" "s/matching_pattern/I+1 d" file.txt | tac #

Example 3: sed delete line match

sed '/pattern to match/d' ./infile

Example 4: sed delete line with match

Use d option at the end of sed command string for deleting line of matches.
sed -i "" "s/matching_pattern/d" file.txt

Tags:

Misc Example