Last active
January 24, 2020 00:48
-
-
Save xinbinhuang/fcf41798881d0cffcc56bdee981da6a5 to your computer and use it in GitHub Desktop.
sed examples to insert, append, and substitute text in your files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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