Summary: This post shares some Unix/Linux
sed command swap/replace examples, where you replace one string or regular expression with another string.
Here's the source code for a sed script that I used to convert some poorly formatted HTML into a format that I preferred. I ran this on a set of almost 30 JSP files from an open source project I recently worked on. The good news is that the previous format of the files was consistent, so I was able to use this sed script to convert all the files at once.
Most of the reformatting lines in the sed script have comments describing what they do, so I won't get into any other detail here. Here is the sed script:
# convert tabs to two spaces s/ / /g # delete leading whitespace (spaces, tabs) from front of each line # aligns all text flush left s/^[ ]*// # add a newline s?<p>\*?\ <p>\*? # put table tag on newline s?<table?\ <table? # get rid of newline? s/<tr>*\n/<tr>/ s/<tr>/ <tr>/ # put </tr> on a newline s?<\/tr>?\ </tr>? # put <td> on a newline s/<td>/\ <td>/g s/<td /\ <td /g # put leading spaces before closing td tag s?^</td? </td? # put on a newline s?<h:selectOneListbox?\ <h:selectOneListbox? s?^<h:selectManyCheckbox? <h:selectManyCheckbox? #s?<h:message? <h:message? # get rid of those pesky colons s?:</td?</td? s?: (? (? # add leading spaces s?<f:? <f:? s?</h? </h? s?^[ ]*<f:view?<f:view? s?^<h:commandButton? <h:commandButton? s?^<input? <input? s?^[ ]*</html>?</html>? s?[ ]*</h3>?</h3>? # add the class tag s?^ <tr>\n <td>? <tr>\ <td class="data_label">?
Some of those sed commands are a little advanced, but I hope you'll be able to figure out what they do. If not, try following this link to the sed man page, or look at some of the other sed posts I have out here.
The shell script I used to run the sed script
I used a Bourne shell script to run my sed script. I used the shell script to help me run these sed commands against every JSP file (*.jsp) in the current directory, converting the HTML of each file. Here's the shell script I used:
#!/bin/sh for i in `ls *.jsp` do echo "Editing $i ..." sed -f pp.sed < $i > $i.tmp mv $i.tmp $i done
I've described this shell script in another blog post, so just follow that link to learn more about how it works.