Ask a question to find trailing space in the entire folder

I have a folder pulled from a wordpress installation> 1000 files. Some php codes throw an error due to trailing whitespace on the last line (I cleaned up the iframe injection on some trailing lines of pages and found / replaced a whitespace, but didn't know it would reset all of my code).

What is the most efficient way to grind these spaces? I've never run a regex before, but I know what I'll be using. Help!

0


a source to share


3 answers


Sort of:

find -type f | xargs sed -i '$ s / [[: space:]] + $ //'



This should remove trailing spaces on the last line of any file anywhere in the cwd. The first $ is the sed address for the "last line" and the second is the regexp for the end of the line.

As always, back up first.

+6


a source


If you are sure that this is the final location on the last line of the file, then you need a bash script (I assume you are using Linux) for each file (use "find." To get the list), tail -1 the file, and then grep this line for space or tab.



It is tangentially useful to know what is closure? > is not needed at the end of PHP files, and it is better to omit it. I can't count the number of times I've seen a PHP file that ends with a "?" And people are going crazy trying to find that extra space that is causing the output / header issues.

+2


a source


in this case I would do is write a script or program that, when given a directory, recursively searches for files ending in .php or whatever, and then just writes a function that 1) searches for the last byte and stores that position 2) if this byte is a space, request back a byte and check again 3) if it is not empty, return the offset

then using that you can either write out each file as a new copy or over the old

PS one of the above solutions will also work: P

0


a source







All Articles