【问题标题】:search Extended asci character in unix在 unix 中搜索扩展的 ascii 字符
【发布时间】:2019-06-11 13:58:30
【问题描述】:

我想搜索一个包含扩展 ascii 字符的字符串,例如 © 或 » 或 ¼ 或 ½。我正在使用这样的 grep 命令搜索上述模式 grep -E '^[a-fA-F0-9\251]+$' 文件

以上代码不工作。

【问题讨论】:

  • 很难做到这一点.. 试试十六进制值

标签: shell unix scripting sh


【解决方案1】:

对相应的扩展字符使用十六进制值。

0xbc for ¼
0xbd for ½
0xa9 for ©

在这种情况下尝试 Perl。这是 ¼ 的示例

$ echo -e "abc\xbcdef" > extended_ascii.txt

$ cat extended_ascii.txt
abc▒def

$ cat -tv extended_ascii.txt
abcM-<def

注意下面的 grep 不匹配

$ grep "\xbc" extended_ascii.txt

Perl 匹配

$ perl -ne ' print if /\xbc/ ' extended_ascii.txt
abc▒def

$

如果你想匹配任何扩展字符,在 ascii 范围之外,那么使用下面的

$ perl -ne ' print if /[^\x20-\x7f]/ ' extended_ascii.txt
abc▒def

$

注意:我的终端显示 ▒ 表示 \xbc,您的终端可能不同。

【讨论】:

    猜你喜欢
    • 2020-08-23
    • 1970-01-01
    • 2011-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-30
    • 2012-07-21
    • 2013-03-19
    相关资源
    最近更新 更多