3

Everyone knows PHP hates blank lines at the beginning or end of a file (before or after the PHP tags).

I've got an awk script that will modify the files. I pass all my files through it and things are peachy, no more leading or trailing blank lines.

I'd like to FIND the files first, to build a quick exception report.

I tried something like this:

grep -r -e :a -e '/^\n*$/{$d;N;};/\n$/ba'

But that's wrong.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
rbellamy
  • 5,683
  • 6
  • 38
  • 48
  • Your '`grep`' command seems to be using `sed`-like constructs (labels, branches, sets of actions on a pattern match). I don't think it will work if `grep` is replaced by `sed` either — but I'm not clear what it is trying to do in the first place. – Jonathan Leffler Sep 08 '13 at 18:03

1 Answers1

13

This shell script will go through all your files and print if it found a blank line at the beginning or end of each file:

for f in `find . -type f`; do 
  for t in head tail; do 
    $t -1 $f  |egrep '^[  ]*$' >/dev/null && echo "blank line at the $t of $f"; 
  done; 
done

I broke the lines for readability, but you can run it as a one liner too.

example output:

blank line at the head of ./b
blank line at the tail of ./c
blank line at the head of ./d
blank line at the tail of ./d
Seth Tisue
  • 29,985
  • 11
  • 82
  • 149
yhager
  • 1,632
  • 15
  • 16
  • Where am I supposed to place this script? I'm running cakePHP on apache web server on windows 7 64 bit professional. – vaanipala Mar 12 '12 at 23:56
  • not sure how to apply this to windows. maybe install cygwin and then run at the base directory of your PHP files? I'm just guessing here.. – yhager Apr 13 '12 at 23:42
  • Thanks very much for this - only suggestion is to change first line to scan only php files: ``for f in `find . -type f -name "*.php"`; do`` – But those new buttons though.. Aug 17 '16 at 23:21
  • this could work if you know your code has only `*.php` files. Some frameworks have `*.inc`, `*.module`, as well. – yhager Aug 18 '16 at 04:51