在关闭后查找额外的空格/新行?>(php标签)

2022-01-20 00:00:00 regex find php unix grep

所以我在关闭 ?>(php 标记)之后有一个空格/换行符,这会破坏我的应用程序.

So I have a space/new line after a closing ?> (php tag) that is breaking my application.

如何轻松找到我在这个应用程序中有 1000 个文件和 100000 行代码.

How can I find it easily I have 1000 of files and 100000 lines of code in this app.

理想情况下,我在一些正则表达式结合 find grep 在 unix 机器上运行之后.

Ideally im after some regex combined with find grep to run on a unix box.

推荐答案

这里的问题是正常的grep不匹配多行.所以,我会安装 pcregrep 并尝试以下命令:

The problem here is normal grep doesn't match multiple lines. So, I would install pcregrep and try the following command:

pcregrep -rMl '?>[s
]+z' *

这将使用 PCRE 多行匹配(-M 部分)匹配文件夹和子文件夹(-r 部分)中的所有文件,并且仅列出它们的文件名(-l 部分).

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).

至于模式,匹配 ?> 后跟 1 个或多个空格或换行符,后跟文件结尾 z.但是我发现,当我在我的文件夹上运行它时,许多 PHP 文件实际上都以一个换行符结尾.因此,您可以将该正则表达式更新为 '?>[s ]+ z' 以匹配单个 字符终止符.

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 ]+ z' to match files with whitespace over and above the single character terminator.

最后,如果您需要检查文件的确切字符序列结尾,您可以随时使用 od -c filename 打印文件的明确表示.

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.

相关文章