-
-
Save radmiraal/fcf1f68e8e72e0ad1aa3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# vim:ft=sh:ts=3:sts=3:sw=3:et: | |
### | |
# Strips the closing php tag `?>` and any following blank lines from the | |
# end of any PHP file in the current working directory and sub-directories. Files | |
# with non-whitespace characters following the closing tag will not be affected. | |
# | |
# Author: Bryan C. Geraghty <bryan@ravensight.org> | |
# Date: 2009-10-28 | |
## | |
if [ -z "$1" ]; then | |
echo "No path given, script usage: ./clean.sh <path>"; | |
exit 1; | |
fi | |
FILES=$(find $1 -name '*.php'); | |
for FILE in $FILES; | |
do | |
MATCH=`pcregrep -rnM '^(\n|\?\>)(?=([\s\n]+)?$(?!\n))' $FILE` | |
if [ "$MATCH" != "" ]; then | |
TARGET=`echo $MATCH | awk -F ':' '{print $1}'`; | |
LINE_COUNT=`wc -l $FILE | awk -F " " '{print $1}'`; | |
echo "Removing lines ${TARGET} through ${LINE_COUNT} from file $FILE..."; | |
sed -i '' "${TARGET},${LINE_COUNT}d" $FILE; | |
fi | |
done; |
tweaked it a bit to remove empty newlines on the end too
@lolli42: thanks ;) Too bad that one didn't turn up in my google search ;)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or on shell in one command (this was typo3 cms, #52360):
find . -type d ( -path ./typo3/contrib/idna -o -path ./typo3/contrib/pear -o -path ./typo3/sysext/adodb/adodb -o -path ./typo3/sysext/openid/lib/php-openid -o -path ./typo3/sysext/openid/lib/php-openid/Auth ) -prune -o -name '*.php' -print | while read FILE; do tail -n1$FILE | grep '^?>$ ' && sed -i '$ d' $FILE; done