Home > bash > sed – substituting certain text

sed – substituting certain text

Problem:
Substitute text in a file only for lines that contain a certain word.

Solution:
# substitute “foo” with “bar” ONLY for lines which contain “baz”
sed ‘/baz/s/foo/bar/g’

Source:
http://www.eng.cam.ac.uk/help/tpl/unix/sed.html

How I used it.
From

Date Total
2011-08-01 15,355
2011-08-02 16,799
2011-08-03 26,521
2011-08-04 41,876
2011-08-05 12,690
2011-08-06 19,489

The raw html. Saved as Aug2011_totals.html

<table>
<tr style="background-color:black;font-family:sans-serif;color:white;font-weight:bold;"><td>Date</td><td>Total</td></tr>
<tr style="background-color:#85c6ef;font-family:sans-serif;text-align:right;"><td style="font-weight:bold;">2011-08-01</td><td>15,355</td></tr>
<tr style="background-color:#d0e3ef;font-family:sans-serif;text-align:right;"><td style="font-weight:bold;">2011-08-02</td><td>16,799</td></tr>
<tr style="background-color:#85c6ef;font-family:sans-serif;text-align:right;"><td style="font-weight:bold;">2011-08-03</td><td>26,521</td></tr>
<tr style="background-color:#d0e3ef;font-family:sans-serif;text-align:right;"><td style="font-weight:bold;">2011-08-04</td><td>41,876</td></tr>
<tr style="background-color:#85c6ef;font-family:sans-serif;text-align:right;"><td style="font-weight:bold;">2011-08-05</td><td>12,690</td></tr>
<tr style="background-color:#d0e3ef;font-family:sans-serif;text-align:right;"><td style="font-weight:bold;">2011-08-06</td><td>19,489</td></tr>

</table>

To

Date Total
2011-08-01 15,355
2011-08-02 16,799
2011-08-03 26,521
2011-08-04 41,876
2011-08-05 12,690
2011-08-06 19,489

The raw html

<table>
<tr style="background-color:black;font-family:sans-serif;color:white;font-weight:bold;"><td>Date</td><td>Total</td></tr>
<tr style="background-color:#85c6ef;font-family:sans-serif;text-align:right;"><td style="font-weight:bold;">2011-08-01</td><td>15,355</td></tr>
<tr style="background-color:#f60516;font-family:sans-serif;text-align:right;"><td style="font-weight:bold;">2011-08-02</td><td>16,799</td></tr>
<tr style="background-color:#f60516;font-family:sans-serif;text-align:right;"><td style="font-weight:bold;">2011-08-03</td><td>26,521</td></tr>
<tr style="background-color:#d0e3ef;font-family:sans-serif;text-align:right;"><td style="font-weight:bold;">2011-08-04</td><td>41,876</td></tr>
<tr style="background-color:#85c6ef;font-family:sans-serif;text-align:right;"><td style="font-weight:bold;">2011-08-05</td><td>12,690</td></tr>
<tr style="background-color:#d0e3ef;font-family:sans-serif;text-align:right;"><td style="font-weight:bold;">2011-08-06</td><td>19,489</td></tr>

</table>

The dates to be highlighted in red may change and to prevent someone from “breaking” something when editing the raw html, it was decided to put the dates in a text file then use sed to make the change to the html file.

shell> less highlighted_dates.txt
2011-08-03
2011-08-02

To do the hightlighting based on the dates in the file. I used sed and bash. This can be then saved as a bash script.

shell> for date_ in $(cat highlighted_dates.txt); do sed -e "/$date_/s/\#85c6ef/\#f60516/" -e "/$date_/s/\#d0e3ef/\#f60516/" Aug2011_totals.html > Aug2011_totals.html.tmp && mv Aug2011_totals.html.tmp Aug2011_totals.html; done

Advertisements
Categories: bash Tags: ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: