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. Ifgrep
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/