find blank lines at end of files

I use Coda for most of my coding needs on the Mac.  I’m also a system’s guy so I use VI a lot to create simple scripts in perl, php and bash. When I’m creating a php script, on of the first things I do is add the opening and closing PHP tags.  One of the drawbacks is that when you use the standard PHP closing tag ‘?>’ and then hit ‘enter’ in your keyboard while using Coda, it adds a blank line to the bottom of your file.  In general this isn’t a problem but your RSS feeds or any XML feed for that matter will more than likely have a blank line before the XML header.  This causes some browsers   – Firefox in particular – to complain about XML syntax.

I needed to find all files that had a blank line at the end.  I love one-liners so here’s one that does the trick:

echo ‘<?php foreach (glob(“**/*.php”) as $file){if (preg_match( “/\\?”.”>\\s\\s+\\Z/m”, file_get_contents($file))) echo(“$file\n”);} ?>’ | php

If you like ruby, here’s a much simpler line:

ruby -e ‘Dir.glob( “**/*.php” ) { |file| puts file if IO.read(file).match( /\?>\s{2,}\Z/m) }’

Advertisement

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 )

Connecting to %s