bash - 通过 Cygwin Grepping 不可打印的字符

标签 bash shell cygwin grep non-printable

Grepping 不可打印字符似乎不适用于回车(控制键 ^M)。

usr@R923047 ~
$ head -3 test.ctl
row 1
row 2
row 3
usr@R923047 ~
$ head -3 test.ctl | cat -nv
     1  row 1^M
     2  row 2^M
     3  row 3
usr@R923047 ~
$ head -3 test.ctl | grep '[^[:print:]]'

usr@R923047 ~
$ head -3 test.ctl | grep '[[:cntrl:]]'

usr@R923047 ~

最佳答案

根据 grep 手册页,您可以将 -U--binary 指定为:

Treat the file(s) as binary. By default, under MS-DOS and MS-Windows, grep guesses the file type by looking at the contents of the first 32KB read from the file. If grep decides the file is a text file, it strips the CR characters from the original file contents (to make regular expressions with ^ and $ work correctly). Specifying -U overrules this guesswork, causing all files to be read and passed to the matching mechanism verbatim; if the file is a text file with CR/LF pairs at the end of each line, this will cause some regular expressions to fail. This option has no effect on platforms other than MS-DOS and MS-Windows.

所以:

$ head -3 test.ctl
row 1
row 2
row 3
$ head -3 test.ctl | cat -nv
     1  row 1^M
     2  row 2^M
     3  row 3
$ head -3 test.ctl | grep '[^[:print:]]'

$ head -3 test.ctl | grep '[[:cntrl:]]'

$ head -3 test.ctl | grep -U '[^[:print:]]'
row 1
row 2

$ head -3 test.ctl | grep -U '[[:cntrl:]]'
row 1
row 2

关于bash - 通过 Cygwin Grepping 不可打印的字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9803211/

相关文章:

linux - Shell 脚本自动回答 Yes No 对话框

shell - 如何使用awk删除第2列和第3列与某些先前匹配的行?

php - Docker 和 php-cli

shell - 进行键绑定(bind)以运行上一个或最后一个 shell 命令

json - 为什么我的 jq 在 JSON 上失败?

c++ - cygwin C++ 静态/可移植库以避免发布源代码?

bash - Cygwin 中的 Rsync 命令失败

Git 将 Emacs 减慢至死亡 - 如何解决这个问题?

linux - 无限循环 shell 脚本没有被杀死

bash - 检查程序中的错误代码-特别是HTK Toolkit