Shell tools, today is sed’s day
Posted: December 4th, 2010 | Author: paul | Filed under: shell | Tags: editing, fun, howto, linux, power, scripting, shell, speed | No Comments »When you have to deal with shell scripting and task automation you need powerful tools.
I will post some basic usage examples to prove how useful sed can be when you have to modify a large number of files.
Here the simple one.
You have several files and you want a word replaced in all of them.
sed -i 's/apache2/httpd/g' files*.conf
This will modify the file matching the pattern files*.conf replacing “apache2″ with “httpd”
Removing -i will print the result on the console.
Now, the really useful one, the one that will save you a lot of time.
The problem you have is: 100+ config files that needs to be modified by adding four lines in a special position in the files.
sed -i '/RewriteEngine/i \
\n<Location \/aaa>\n\tProxyPass ajp\:\/\/127.0.0.1\:8019\/aaa\n\tProxyPassReverse ajp\:\/\/127.0.0.1\:8019\/aaa\n\<\/Location\>\n' files*.conf
-i will modify the files by adding before “RewriteEngine" the text:
empty line
<Location/aaa>
<tab>ProxyPass ajp://127.0.0.1:8019/aaa
<tab>ProxyPassReverse ajp://127.0.0.1:8019/aaa
</Location>
emptyline
Now think about adding these lines in every single file, one by one, from 1 to 100, each in an exact position, some of the configs having two or more sections to be added.
Piece of cake with sed.
Recent Comments