如何查找不以换行符/换行符结尾的文件? [英] How do I find files that do not end with a newline/linefeed?
问题描述
如何列出不以换行符结尾的普通文本(.txt
)文件名?
How can I list normal text (.txt
) filenames, that don't end with a newline?
例如:列出(输出)此文件名:
e.g.: list (output) this filename:
$ cat a.txt
asdfasdlsad4randomcharsf
asdfasdfaasdf43randomcharssdf
$
不要列出(输出)该文件名:
and don't list (output) this filename:
$ cat b.txt
asdfasdlsad4randomcharsf
asdfasdfaasdf43randomcharssdf
$
推荐答案
使用pcregrep,它是grep的Perl兼容正则表达式版本,它支持使用-M标志的多行模式,该标志可用于在以下情况下匹配(或不匹配):最后一行有换行符:
Use pcregrep, a Perl Compatible Regular Expressions version of grep which supports a multiline mode using -M flag that can be used to match (or not match) if the last line had a newline:
pcregrep -LMr '\n$' .
在上面的示例中,我们说要在当前目录(.
)中递归搜索(-r
),列出与(-L
)我们多行(-M
)正则表达式不匹配的文件,该正则表达式查找文件末尾的换行符('\n$'
)
In the above example we are saying to search recursively (-r
) in current directory (.
) listing files that don't match (-L
) our multiline (-M
) regex that looks for a newline at the end of a file ('\n$'
)
将-L
更改为-l
会列出做中包含换行符的文件.
Changing -L
to -l
would list the files that do have newlines in them.
pcregrep
可以使用Homebrew pcre
软件包:brew install pcre
pcregrep
can be installed on MacOS with the homebrew pcre
package: brew install pcre
这篇关于如何查找不以换行符/换行符结尾的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!