sed
stream editor for filtering and transforming text.
Table of Contents
Delete lines using line numbers
# delete single line
sed -i '' -e '27d' ~/.ssh/known_hosts
# on FreeBSD / OS X
sed -i '' -e '27d' ~/.ssh/known_hosts
Print alternate lines in file
# Print Odd Numbered Lines,
sed -n 'p;n'
# Print Even Numbered Lines,
sed -n 'n;p'
Search and replace text
# preview output
$ git ls-files -- themes/terminal/assets/ | \
xargs sed -En "s/(theme)-(dark|light)/\1--\2/p"
bodyClassList.add("theme--dark");
bodyClassList.add("theme--light");
bodyClassList.add("theme--dark");
# search and replce in place
git ls-files -- themes/terminal/assets/ | \
xargs sed -Ei "s/(theme)-(dark|light)/\1--\2/"
Extract numbers at the end of line
Example: extract shellcheck error codes
$ git grep -n "shellcheck" dot-bashrc | \
sed -nE "s/.*=(SC)?([0-9]+)$/\1\2/p" | sort -u
1087
2034
2154
2296
SC1090
SC1091
SC2016
SC2034
SC2120