Skip to content

Instantly share code, notes, and snippets.

@DevGW
Created March 13, 2023 16:15
Show Gist options
  • Save DevGW/219dc6d1eaa21784f481e0f67478d205 to your computer and use it in GitHub Desktop.
Save DevGW/219dc6d1eaa21784f481e0f67478d205 to your computer and use it in GitHub Desktop.
Sed Cheatsheet #hha_ank #cheatsheet #bash #sed

In place replacements

In-place replacement (GNU)

sed -i -e 's/foo/bar/' example.md

In GNU sed: use -i without arg.

In-place replacement (BSD)

sed -i '' -e 's/foo/bar/' example.md

In OSX, -i '' is required.

File regions

Print until a certain line is met

sed '/begin api/q'

Print until a certain line is met, but not that line

sed '/^# begin/,$d'

Print everything after a given line

sed -n '/end api/,$p'

Print after a given line is found.

Print everything except matching

sed -n '/regex/!p'

Print everything except lines matching regex. Useful for printing files with comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment