Skip to content

Instantly share code, notes, and snippets.

@xinbinhuang
Last active January 24, 2020 00:48
Show Gist options
  • Save xinbinhuang/fcf41798881d0cffcc56bdee981da6a5 to your computer and use it in GitHub Desktop.
Save xinbinhuang/fcf41798881d0cffcc56bdee981da6a5 to your computer and use it in GitHub Desktop.
sed examples to insert, append, and substitute text in your files
sed -i \ # -i: inplace; you can remove -i to do a dry run
-e '/<pattern>/i <text>' \ # /i: insert text after the pattern
-e '/<pattern>/a <text>' \ # /a: append text before the pattern
-e 's/<pattern>/<text>/g' \ # s/: substitute ; /g: global; match ALL instances instead of just the 1st match
-e 's/(<pat1>)<random_text>(<pat2>)/\1 + \2' # \1 reference <pat1>; \2 reference <pat2>
input.file
# to loop through a directory
find <directory> -type f -exec sed -i -e 's/<pattern>/<text>/g' {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment