Monthly Archives: November 2015

Last updated by at .

PERL Search and Replace in Multiple Files – Easy as PIE

I help to administer a blog with about 2000 entries.  Previously the site was managed in MovableType but earlier this year we moved it over to WordPress.  At the time we migrated about 30% of the posts to WordPress (the top trafficked 30%).  It was a slow process and as time has gone by we’ve migrated more and more posts  but it’s a time consuming process.  We need to create the new WordPress post, put a 301 redirect in place and then manually delete the old MovableType post.  And by manual deletion I mean deleting the actual HTML file that MoveableType created for that post.  You see, MovableType produces static files for each and every post, archive page, and index page.  This is a blessing and a curse, it means that the load on the web server isn’t large on heavily trafficked sites, but it also means maintaining a legacy site is a PAIN IN THE A**.  It’s actually such a pain that I have given up with MT itself and now I am working with the static HTML files directly.

This week I needed an easy way of doing a search and replace in all the legacy HTML files, all 2000 or so of them.  It needed to be recursive and ideally it needed to happen without me needing to FTP all the files to a local computer and then FTP them back.  I have command line access to the server the blog is on so I checked to see if PERL offered a way to do what I wanted working on the files in situ.  As it turns out it’s pretty simple.  I wanted to find and replace all internal links that used the old domain name (did I mention this site just changed domain names) with the new domain name.  The PERL command to do this with all the files in a single folder is like this:

perl -p -i -e 's/oldstring/newstring/g;' *.html

The -p means the script we’re running will be put through the C pre-processor before PERL compilation. The -i option means that PERL will edit the files in place. The -e option allows the running of PERL commands from the command line (it doesn’t look for a script file). The actual PERL operator s is the substitution operator while the he oldstring and newstring are regular expressions so special characters need to be escaped appropriately. And finally the operator /g means the command will do a global match. So to replace my URLs I needed something like this:

perl -p -i -e 's/www\.old\-domain\.com/www\.new\-domain\.com/g' *.html

The issue with that is that I needed the script to process sub-directories recursively to search and replace in all the HTML files. That could be done in a few different ways, the immediately obvious were chaining with FIND or GREP. I chose FIND and ended up with a command that looks like this:

perl -p -i -e 's/www\.old\-domain\.com/www\.new\-domain\.com/g' ' `find ./ -name "*.html"`

Ran that from the ubuntu commmand line and thousand or more files were processed in under a second. Very cool.

Brew Day 12 – Reddit /r/HomeBrew Collaboration APA

I often frequent the /r/HomeBrewing sub-reddit and over the last few weeks a member has been developing an APA recipe based on feedback from the sub-reddit subscribers. You can view the recipe here, it’s a pale ale using 2-row, Maris Otter, some oats, and hopped with Warrior, Amarillo, and Citra. As I mostly brew in isolation (apart from my lovely partner who is amusingly tolerant of my brewing fascination) I thought it would be fun to take part in the group-think and have a go at the recipe myself. However, I wanted to try a no-chill (which seemed to work really well last time) and a shorter boil. Aim was to be done with brew day in about 2 hours.

I had to scale down the recipe for my usual 12.5L size (the recipe calls for 5.5 gallons) and adjust the hop schedule to account for the no-chill approach. I’ve done quite a bit of reading in the last few weeks and the rule of thumb suggested is that flame-out additions for no chill should be counted as 10-15 minute additions. The recipe was constructed in the very handy BIAB Beer Designer spreadsheet.

Ale Barret Burston 2 Row 2.04kg
Ale Maris Otter 0.58kg
CaraPils 0.14kg
Caramalt Malt (50EBC) 0.14kg
Oats Flaked 0.14kg

Magnum 9g (12.7%AA) @ 30 minutes
Amarillo 5g (9.5% AA) @ 5 minutes
Citra 5g (13.2% AA) @ 5 minutes
Amarillo 13g @ Flameout
Citra 13g @ Flameout
Amarillo 35g Dry Hop 3 days
Citra 44g Dry Hop3 days




Brew Type : 12.5L All Grain (BIAB)

OG: 1.052
FG: 1.014
ABV: 5.0%
IBU: 19.9
EBC: 11.4

You’ll note that the IBU’s are just under 20, but if you dial in the 5 minute and flameout additions as 10 minute additions you arrive at 42IBU which is right on what the original recipe calls for. The logic for this may be a little flawed as I suspect the 5 minute additions should really be considered as 15 minute additions but the proof will be in the tasting.

Brew Day (21 November 2015)

The evening before I filled my brew pot with 10L of water and my sparge pot with 7L of water and left them sitting to allow for any volatiles to evaporate overnight.

Heating Strike Water

Heating Strike Water

1. Brew day started at 6:40AM when I brought 10L of water to strike temperature of 71C with grain bag lining pot.

2. Added grain bill, stir to ensure no dough balls.

Mashing In!

Mashing In!

3. Took temperature of mash (69.0C) replaced lid on pot, and wrapped pot in doona/blanket for 60 minutes to mash.

There's Wort in There

There’s Wort in There

4. Bring 7L of water to 75C in another pot.

5. At the end of 60 minutes unwrap pot, take temperature again (68.0C). I drained the bag on a wire rack suspended over the brew pot for several minutes.

Draining the Bag

Draining the Bag

6. Sparged the bag with the 75C water until there was a litre or two left and put the bag in the sparge pot and let it rinse out the last of the wort.

Bringing Wort to the Boil

Bringing Wort to the Boil

7. At this stage I had 13L of wort in the brew pot. I took a gravity reading (1.037 @ 61.5C), added a few drops of FERMCAPs to stop boil-overs and brought it all to the boil.

Hop Additions

Hop Additions

8. Hop additions were made when the boil started (Warrior), 25 minutes (5g of Citra and Amarillo) and at the end of the 30 minute boil (13g each of Citra and Amarillo). At this point I whirlpooled the wort for 10 minutes.

Whirlpooling at Flameout

Whirlpooling at Flameout

9. Once that time was up I put the pot into my pre-chilled fridge to bring the temperature down to pitching temp (19C). I expected that to take about 12 hours and aimed to pitch my yeast the next morning. At this point it was 9.20AM, 2 hours and 40 minutes since I’d started. In that time I’d brewed the beer, showered and breakfasted, and answered my morning work emails. Generally I was pretty happy with how short the whole exercise was.

Hot Wort Cooling in Fridge

Hot Wort Cooling in Fridge

10. (Morning of 22 November 2015). I drained the wort into my sterilised fermentation vessel trying to leave as much hop residue as possible in the brew pot. I had to top it off with about 2 liters of cooled boiled water to get my 12.5L volume. I took a gravity reading (1.053) and temperature reading (19C). Aerated thoroughly with a large (sterilised) plastic spoon and pitched my yeast. Filled the airlock with steriliser, put the lit on the FV and put it in my fridge with a set temperature of 19C.

Yeast is Pitched

Yeast is Pitched

Generally I was happy with the day, my mashing temperature was a little high but it doesn’t seem to have effected anything as I achieved my usual efficiency and just missed the target OG by one lousy point. The wort was a very pale green/gold, without doubt the palest wort I have made to date. Not sure if this was a product of the grain bill or the shorter boil. I certainly liked the shorter boil time, however the longest periods of inactivity (other than the mash) was spent waiting for the strike water to heat up and bringing the wort to the boil. That idle time is only going to be solved by getting a bigger gas burner which isn’t on the cards any time soon.

Mash Efficiency

I did my mash efficency calculations in Excel, and got my usual efficency of 70%.

Kg Potential Pounds Potential Points
Ale Barret Burston 2 Row 2.04 1.038 4.49 49.59
Ale Maris Otter 0.58 1.038 1.28 14.10
CaraPils 0.14 1.033 0.31 2.96
Caramalt Malt 0.14 1.034 0.31 3.04
Oats Flaked 0.14 1.037 0.31 3.31
Potential 73.00
      Actual 51.00
      Efficency 69.9%


Schofferhofer Hefeweizen Review

Schofferhofer Hefeweizen

Schofferhofer Hefeweizen

To the left you can see a bottle of Schofferhofer Hefeweizen, an unfiltered German wheat beer. As you can see the beer is a cloudy orange gold colour and had a nice finely bubbled head. On the nose it is earthy and a bit funky, no doubt due to the use of some sort of noble hop. It has a metallic rounded taste (which I assume is the wheat used in the grist) with a lingering breadyness in my mouth at the back once the mouthful is complete. Bitterness is not particular strong and I don’t sense any real hop flavour. I’m not sure I’m enjoying the glass too much and don’t think I’ll rush out to brew this style just yet.

Reading some online reviews of the beer there’s mention of banana and cloves but I get none of that. Compare that with the Weizen Doppelbock I had at Redoak Brewery last month which was the most strongly banana flavoured thing I’ve ever had that wasn’t actually a banana.

4 Pines Brewing Kolsch Review

4 Pines Brewing Kolsch

4 Pines Brewing Kolsch

I’ve been looking for a style of ale to brew for the summer months and a kolsch is among the styles I was considering. Given that I’ve never actually had one I thought I’d give it a try and found a Kolsch marketed as a German Style Golden Ale by 4 Pines Brewing of Manly in Sydney. The beer was remarkably clear with a rich golden colour and only a thin head that dispersed quite quickly. On the nose it was earthy with perhaps a slight hint of pine. On the palate it was faintly malty with a nice bit of refreshing bitterness but not particular dry. After taste was a little spicy with lingering bitterness. I certainly drank it quickly enough on a hot day so I’ll call it quite quaffable. The ABV of 4.7% means I could have a few without feeling the need to dance on a table for my own amusement. It’s certainly a style of beer I’ll look at brewing myself.

wpDataTables Filters in WordPress

I’ve started using wpDataTables for data driven tables in WordPress. The ability to have a table linked to an Excel spreadsheet is neat and the automagically formatted tables that use the jQuery Data Tables plugin are nice too. I needed to tweak few things though. First, center align the table titles. Easy enough using the wpdatatables_filter_rendered_table


function my_filter_rendered_table( $table_content, $table_id )
	$content=str_replace('<h2>','<h2 style="text-align:center;">',$content);
	return $content;

The second issue I was having was with URL Link Columns. These are driven from an Excel spreadsheet by separating the URL and the link text with two pipe (|) characters. Something like:||Nifty Link Text

In the case of the data I was displaying some of the cells would need links and some wouldn’t. Trouble was wpDataTables appears to render every value in a URL Link Column as a link whether you pass it a URL or not. In fact, it uses the Link Text as the URL if you omit the URL. Instant broken links, yuck. No problems, I thought initially, I’d just use the provided wpdatatables_filter_link_cell filter to grab the URL links, parse out the URL and the link text and just return the link text if the URL was invalid. Pretty easily done with a bit of regex like:

	$url=preg_match('/href="(.*?)"/', $link, $url_matches);
	$link_text=preg_match('/>(.*?)<\/a>/', $content, $link_text_match);

It worked beautifully for the Link Text but rather frustratingly preg_match stubbornly returned 0 (no match) for the URL no matter what I tried. Even when I VAR_DUMP’ed the link it appeared that the preg_match should work. But it didn’t. In the end I used substr to echo back the link one character at a time and hey presto, wpDataTables was wrapping the href parameter in a single quote rather than a double quote. When I echoed the value to output using VAR_DUMP the browser (un)helpfully replaced the single quotes with double quotes. Grrrr. Anyway here’s the code that worked:


function my_filter_link_cells($link)
	$url=preg_match("/href='(.*?)'/", $link, $url_matches);
	$link_text=preg_match('/>(.*?)<\/a>/', $content, $link_text_match);
	if (strpos($url_matches[1],"/")===false || strpos($url_matches[1]," ")!==false)
	return $content;

Now my tables displayed links for values that actually had a proper URL and just text for those that didn’t. Yay.

wpDataTables seems pretty good, it isn’t free but $29 seems like a decent price for something that has worked pretty much exactly as advertised and has a tonne of features. Recommended.