Posted by CANbike on Sat, 10 Oct 2015

sed: Delete All Lines Before or After a Keyword

sed is a powerful text processing utility that can be used to delete all lines before a matching string, or all lines after a matching string.

The following are three examples of sed.


Delete All Lines Before a Keyword with sed

A range from the “KEYWORD” to the last line in the input stream can be located and not deleted it with following command,

sed '/KEYWORD/,$!d'

where

,    - defines a range
$    - special character representing the last line in the input stream
!    - NOT
d    - delete command

sed-Delete All Lines Before a match.png


Delete All Lines After a Keyword with sed

After finding a line containing the “KEYWORD” the stream can be quit, with the following command,

sed '/KEYWORD/q'

where

q    - quit

sed-Delete All Lines After a Match.png


Delete All Lines Including the Keyword and After the Keyword with sed

A range from the “KEYWORD” to the last line in the input stream can be located and deleted by,

sed '/KEYWORD/,$d'

where

,    - defines a range
$    - special character representing the last line in the input stream
d    - delete command

sed-Delete All Lines Including the Match and After the Match.png


Related Item(s):