So I have a space/new line after a closing ?>
(php tag) that is breaking my application.
How can I find it easily I have 1000 of files and 100000 lin
use perl;
perl -0777 -i -pe 's/\s*$//s' *.php
s/\s*$//s - treat all lines as a single line and substitute any space at the end to nothing
This works for me...
<\?php | \?>
If you need to use in in a sublime-settings file or something like that which doesn't like forward slashes, you might have to add an extra slash for each of them like so...
<\\?php | \\?>
Hope that helps!
Using notepad++ you can replace easily all documents at the same time, drap and drop that folder of files and press CTRL + R, also you can use Regex
The problem here is normal grep doesn't match multiple lines. So, I would install pcregrep
and try the following command:
pcregrep -rMl '\?>[\s\n]+\z' *
This will match all files in the folder and subfolders (the -r
part) using PCRE multiline match (the -M
part), and only list their filenames (the -l
part).
As for the pattern, well that matches ?>
followed by 1 or more whitespace or newline characters, followed by the end of the file \z
. I found though, when I ran this on my folder, many of the PHP files do in fact end with a single newline. So you can update that regex to be '\?>[\s\n]+\n\z'
to match files with whitespace over and above the single \n
character terminator.
Lastly, you can always use od -c filename
to print unambiguous representation of the file if you need to check its exact character sequence ending.
grep '?> ' *.php
? Of course, it may not be a space and could be a linebreak or a tab, so you may want to try other characters.
This worked for me to find white spaces before php files
find -name '*.php' | xargs grep -Pz '\?>[\s]+$' -l