在 BOF 或 EOF 中查找所有带有空白或 WS 的文件

2021-12-24 00:00:00 shell sed php grep

每个人都知道 PHP 讨厌在文件的开头或结尾(在 PHP 标签之前或之后)出现空行.

Everyone knows PHP hates blank lines at the beginning or end of a file (before or after the PHP tags).

我有一个 awk 脚本可以修改文件.我通过它传递了我的所有文件,一切都很好,没有更多的前导或尾随空行.

I've got an awk script that will modify the files. I pass all my files through it and things are peachy, no more leading or trailing blank lines.

我想先查找文件,以构建快速异常报告.

I'd like to FIND the files first, to build a quick exception report.

我尝试过这样的事情:

grep -r -e :a -e '/^
*$/{$d;N;};/
$/ba'

但那是错误的.

推荐答案

此 shell 脚本将遍历您的所有文件,并在每个文件的开头或结尾找到空行时进行打印:

This shell script will go through all your files and print if it found a blank line at the beginning or end of each file:

for f in `find . -type f`; do 
  for t in head tail; do 
    $t -1 $f  |egrep '^[  ]*$' >/dev/null && echo "blank line at the $t of $f"; 
  done; 
done

为了可读性我打破了线条,但你也可以将它作为单行运行.

I broke the lines for readability, but you can run it as a one liner too.

示例输出:

blank line at the head of ./b
blank line at the tail of ./c
blank line at the head of ./d
blank line at the tail of ./d

相关文章