Find extra space / new line after a closing ?> (php tag)

后端 未结 9 1656
醉话见心
醉话见心 2020-12-17 18:04

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

相关标签:
9条回答
  • This works on my box:

    for i in `find . -name "*.php"`; do (echo -n "$i: "; tail -c 3 $i) | grep -v "[?]>"; done
    

    The idea is that you take just the last 3 characters with tail, then discard the files where those are '?', '>' and newline. If there's a space or another newline, you won't get the '?' character..

    0 讨论(0)
  • 2020-12-17 19:04

    This is possible with regular grep

    grep -Pz '\?>[\s]+$' -Rl
    

    Will search for all files starting from the current directory and list all that have a ?> followed by white space at the end of the file.

    • -P Interpret the pattern as a Perl-compatible regular expression (PCRE).
    • -z Treats the input file as one long line - this is in part what makes it work
    • [\s]+ matches at least one white space - including newlines

    If you want to match PHP files only:

    find -name '*.php' | xargs grep -Pz '\?>[\s]+$' -l
    

    To search for white space at the beginning of the file before

    find -name '*.php' | xargs grep -Pz '^[\s]+<\?' -l
    
    0 讨论(0)
  • 2020-12-17 19:07
    sed -e :a -e '/^[ \n]*$/{$d;N;ba' -e '}' -e '$ s/\([^ ]\)*?>[ ]*/\1?>/' file.php > new_file.php
    

    to be executed for each file not completely tested..

    remember to work on a temporary file and after the sed operation copy the new file on the original one..

    0 讨论(0)
提交回复
热议问题