Skip to content

Instantly share code, notes, and snippets.

@heitara
Last active April 12, 2024 20:41
Show Gist options
  • Save heitara/25e13f82e1ebd73e16dea5e33a62d00a to your computer and use it in GitHub Desktop.
Save heitara/25e13f82e1ebd73e16dea5e33a62d00a to your computer and use it in GitHub Desktop.
Useful sed commands for macOS

Here are some useful sed commands for file editing

sed 10q demo.txt - prints first 10 lines

sed -i'.bak' 's/hello/bar/' demo.txt - replace the first "hello" on each line with "bar"

sed -i'.bak' 's/hello/bar/g' demo.txt - replace the ALL "hello" on each line with "bar"

sed -i'.bak' '3d' demo.txt - remove the 3rd line in a file

sed -i '' -e "2s/^//p; 2s/^.*/var exports = {};/" demo.txt - insert a new line on the 3rd row. (duplicate 2nd and edit the copy) (do it inplace)

sed -i '' '3i\'$'\n''var exports = {};' demo.txt - insert at the beginning of the 3rd line (do it inplace)

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