Docu review done: Thu 29 Jun 2023 12:19:36 CEST

Table of Content

Commands

CommandDescription
sed '/^$/d'removes all empty lines
sed '/[REGEX]/!d'removes all notmatching lines
sed -n -e 'H;${x;s/\n/,/g;s/^,//;p;}'replaces newline with ,

Remove lines by numbers

CommandDescription
sed -n '1p'shows specific line on number x
sed '1d'removes first line
sed '4d'removes fourth line
sed '$d'removes last line
sed '2,4d'removes second till fourth line
sed '2,4!d'removes all lines except specified rang

Remove lines by regex

CommandDescription
sed -[eE] '/<RegEx>/d'removes all lines where regex matchg
sed -[eE] '/<RegEx>/!d'removes all lines where regex NOT matchg

Remove lines by regex and/or next line

$ sed -[eE] '/<RegEx>/{N;d;}'         # removes the matching lines and the next line
$ sed -[eE] '/<RegEx>/{N;s/\n.*//;}'  # removes the next line after the matching

Remove lines by regex and/or previouse line

$ sed -n -[eE] '/<RegEx>/{s/.*//;x;d;};x;p;${x;p;}' | sed '/^$/d'   # removes the matching lines and the previouse line
$ sed -n -[eE] '/<RegEx>/{x;d;};1h;1!{x;p;};${x;p;}'                # removes only the previouse line of the mathing

Remove lines by regex, the previouse line and next line

$ sed -n -[eE] '/<RegEx>/{N;s/.*//;x;d;};x;p;${x;p;}' | sed '/^$/d' # removes the matching lines, the prevoise ones and the next ones