Tag Archives: sed

Using ‘sed’ to repace text in a file

I have 100 .svg files in a directory that I wanted to crop by one pixel on each side. This was an automatically generated 1 px wide white border, so it was controlled by only a single line in the .svg file specifying a white box from 0,0 to 31,31. The hard way would be to use Inkscape to modify each file individually, but once again ‘sed’ can come to the rescue and modify all 100 files with one command.

The pertinent line that I needed to change was

<rect x=”0″ y=”0″ width=”31″ height=”31″ fill=”#FFFFFF” />
to
<rect x=”1″ y=”1″ width=”29″ height=”29″ fill=”#FFFFFF” />

so we invoke ‘sed’ with


# sed -i 's/x="0" y="0" width="31" height="31"/x="1" y="1" width="29" height="29"/1' *.svg

The ‘-i’ switch tells ‘sed’ to modify the files in place and the ‘s/regexp/replacement/’ defines the string to search for and replace. the ‘/1′ switch on the end tells ‘sed’ to operate only on the 1st instance of the matched expression.

Leave out the ‘-i’ switch and operate on a single file to preview the changes to stdout before committing.

And voila! 100 files fixed in no time!

Using ‘sed’ to bulk edit text files

Google Analytics provides free yet super-sophisticated website tracking and statistics. Unfortunately, it requires that the following code snippet be inserted into every web page immediately before the </body> tag.


<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-12232036-2");
pageTracker._trackPageview();
} catch(err) {}</script>

Now with a static website such as www.elizabethpassela.com, that’s a lot of pages to manually update. What we’d like is a way to perform a bulk insert of the required text on all .htm files in the webroot directory. Here’s where ‘sed’ comes to the rescue.

First task is to create a ‘sed’ script file called sed.cmd. The first line includes the search string </body> and the /i “insert” option which tells ‘sed’ to insert the following text before the </body> string. The ” at the end of each line is needed to tell ‘sed’ that another line exists in the script, sort of a continuation operator. Since the ‘/’ character was used as the separator for the script command, all ‘/’ characters which are part of the operand need to be escaped with ”.


/</body>/i
<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-12232036-2");
pageTracker._trackPageview();
} catch(err) {}</script>

Finally, execute the following command to insert the text into all .htm files. The '-i' tells sed to operate on the files in-place (if 'sed' is run without the '-i' option, the output will be sent to stdout which is good for testing).


# sed -i -f ~cbattles/sed.cmd *.htm

A similar approach can be used for replacing and appending text as well as inserting. Additionally, the 'find command can be piped to 'sed' to operate on selective files.


Caveat lector — All work and ideas presented here may not be accurate and should be verified before application.