加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

bash – 由Cygwin打印不可打印的字符

发布时间:2020-12-15 21:07:25 所属栏目:安全 来源:网络整理
导读:Grepping不可打印的字符似乎不适用于回车(控制键^ M). usr@R923047 ~$head -3 test.ctlrow 1row 2row 3usr@R923047 ~$head -3 test.ctl | cat -nv 1 row 1^M 2 row 2^M 3 row 3usr@R923047 ~$head -3 test.ctl | grep '[^[:print:]]'usr@R923047 ~$head -3 t
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 man-page,您可以指定-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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读