Practical sed commands
I am posting a summary of my commands after a bit of Googling and after playing on my CentOS VM with sed.
WARNING & ADVICE:
1. Always backup your files before playing with sed
2.The -i option in sed will replace the original file. So I recommend you run your
sed commands without the -i option first. Once you get the desired results of your sed command you
can then use the -i option.
append after <body>
sed '/<body>/a Hello World' sample.html
sed -i '/<body>/a Hello World' sample.html
append contents of file header-js.txt after <body>
sed -i '/<body>/ r header-js.txt' sample.html
sed -i '/<\/body>/ r footer-js.txt' sample.html
Some tests before proceeding further (tip: don't ignore the single quote)
[root@localhost mydir]# find . -name '*.html'
./sample2.html
./sample3.html
./sample.html
combine find with sed for mass modifications
find . -iname '*.html' -exec sed -i '/<body>/ r header-js.txt' '{}' \;
find . -iname '*.html' -exec sed -i '/<\/body>/ r footer-js.txt' '{}' \;
append at beginning of file
sed -i '1s/^/my script goes here\n/' file
WARNING & ADVICE:
1. Always backup your files before playing with sed
2.The -i option in sed will replace the original file. So I recommend you run your
sed commands without the -i option first. Once you get the desired results of your sed command you
can then use the -i option.
append after <body>
sed '/<body>/a Hello World' sample.html
sed -i '/<body>/a Hello World' sample.html
append contents of file header-js.txt after <body>
sed -i '/<body>/ r header-js.txt' sample.html
sed -i '/<\/body>/ r footer-js.txt' sample.html
Some tests before proceeding further (tip: don't ignore the single quote)
[root@localhost mydir]# find . -name '*.html'
./sample2.html
./sample3.html
./sample.html
combine find with sed for mass modifications
find . -iname '*.html' -exec sed -i '/<body>/ r header-js.txt' '{}' \;
find . -iname '*.html' -exec sed -i '/<\/body>/ r footer-js.txt' '{}' \;
append at beginning of file
sed -i '1s/^/my script goes here\n/' file