AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 730591
Accepted
Doug Smythies
Doug Smythies
Asked: 2016-02-07 13:54:19 +0800 CST2016-02-07 13:54:19 +0800 CST 2016-02-07 13:54:19 +0800 CST

ASCII 源文件检查器

  • 772

对于源英文文件位于 docbook xml 中的官方 Ubuntu 文档,要求仅使用 ASCII 字符。我们使用“检查器”命令行(参见此处)。

grep --color='auto' -P -n "[\x80-\xFF]" *.xml

但是,该命令有一个缺陷,显然不是在所有计算机上,它都会遗漏一些带有非 ASCII 字符的行,从而可能导致错误的 OK 结果。

有人对 ASCII 检查器命令行有更好的建议吗?

有兴趣的人可以考虑使用这个文件(文本文件,而不是 docbook xml 文件)作为测试用例。非 ASCII 字符的前三行是第 9、14 和 18 行。检查中遗漏了第 14 和 18 行:

$ grep --color='auto' -P -n "[\x80-\xFF]" install.en.txt | head -13
9:Appendix F, GNU General Public License.
330:when things go wrong. The Installation Howto can be found in Appendix A,
337:Chapter 1. Welcome to Ubuntu
359:1.1. What is Ubuntu?
394:1.1.1. Sponsorship by Canonical
402:1.2. What is Debian?
456:1.2.1. Ubuntu and Debian
461:1.2.1.1. Package selection
475:1.2.1.2. Releases
501:1.2.1.3. Development community
520:1.2.1.4. Freedom and Philosophy
534:1.2.1.5. Ubuntu and other Debian derivatives
555:1.3. What is GNU/Linux?
command-line
  • 3 3 个回答
  • 5400 Views

3 个回答

  • Voted
  1. Byte Commander
    2016-02-07T15:03:24+08:002016-02-07T15:03:24+08:00

    您可以使用我在 GitHub 上托管的 Python 3 脚本打印文件的所有非 ASCII 行:

    GitHub: ByteCommander/编码检查

    您可以克隆或下载整个存储库,也可以简单地保存文件encoding-check并使用chmod +x encoding-check.

    然后你可以像这样运行它,将要检查的文件作为唯一参数:

    • ./encoding-check FILENAME如果它位于您当前的工作目录中,或者...
    • /path/to/encoding-check FILENAME如果它位于/path/to/,或者...
    • encoding-check FILENAME如果它位于作为$PATH环境变量一部分的目录中,即/usr/local/bin或~/bin.

    如果没有任何可选参数,它将打印找到非 ASCII 字符的每一行及其编号。最后,有一个摘要行告诉您文件总共有多少行以及其中有多少行包含非 ASCII 字符。

    此方法保证正确解码所有 ASCII 字符并检测绝对不是 ASCII 的所有内容。

    这是一个在包含您给定的前 20 行的文件上运行的示例install.en.txt:

    $ ./encoding-check install-first20.en.txt
         9: Appendix��F, GNU General Public License.
        14: (codename "���Xenial Xerus���"), for the 64-bit PC ("amd64") architecture. It also
        18: ���������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
    --------------------------------------------------------------------------------
    20 lines in 'install-first20.en.txt', thereof 3 lines with non-ASCII characters.
    

    但是脚本有一些额外的参数来调整检查的编码和输出格式。查看帮助并尝试:

    $ encoding-check -h
    usage: encoding-check [-h] [-e ENCODING] [-s | -c | -l] [-m] [-w] [-n] [-f N]
                         [-t]
                         FILE [FILE ...]
    
    Show all lines of a FILE containing characters that don't match the selected
    ENCODING.
    
    positional arguments:
      FILE                  the file to be examined
    
    optional arguments:
      -h, --help            show this help message and exit
      -e ENCODING, --encoding ENCODING
                            file encoding to test (default 'ascii')
      -s, --summary         only print the summary
      -c, --count           only print the detected line count
      -l, --lines           only print the detected lines
      -m, --only-matching   hide files without matching lines from output
      -w, --no-warnings     hide warnings from output
      -n, --no-numbers      do not show line numbers in output
      -f N, --fit-width N   trim lines to N characters, or terminal width if N=0;
                            non-printable characters like tabs will be removed
      -t, --title           print title line above each file
    

    因为--encoding,Python 3 知道的每个编解码器都是有效的。试一试,在最坏的情况下,您会收到一些错误消息...

    • 7
  2. Best Answer
    muru
    2016-02-07T22:51:11+08:002016-02-07T22:51:11+08:00

    如果您想查找非 ASCII 字符,也许您应该反转搜索以排除 ASCII 字符:

    grep -Pn '[^\x00-\x7F]'
    

    例如:

    $ curl https://help.ubuntu.com/16.04/installation-guide/amd64/install.en.txt -s | grep -nP '[^\x00-\x7F]' | head
    9:Appendix F, GNU General Public License.
    14:(codename "‘Xenial Xerus’"), for the 64-bit PC ("amd64") architecture. It also
    18:━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    330:when things go wrong. The Installation Howto can be found in Appendix A,
    337:Chapter 1. Welcome to Ubuntu
    359:1.1. What is Ubuntu?
    368:  • Ubuntu will always be free of charge, and there is no extra fee for the "
    372:  • Ubuntu includes the very best in translations and accessibility
    376:  • Ubuntu is shipped in stable and regular release cycles; a new release will
    380:  • Ubuntu is entirely committed to the principles of open source software
    

    在第 9、330、337 和 359 行中,存在Unicode 不间断空格字符。


    您得到的特定输出可能是由于grep对 UTF-8 的支持。对于 Unicode 语言环境,其中一些字符可能与普通的 ASCII 字符比较相等。在这种情况下,强制 C 语言环境将显示预期结果:

    $ LANG=C grep -Pn '[\x80-\xFF]' install.en.txt| head
    9:Appendix F, GNU General Public License.
    14:(codename "‘Xenial Xerus’"), for the 64-bit PC ("amd64") architecture. It also
    18:━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
    330:when things go wrong. The Installation Howto can be found in Appendix A,
    337:Chapter 1. Welcome to Ubuntu
    359:1.1. What is Ubuntu?
    368:  • Ubuntu will always be free of charge, and there is no extra fee for the "
    372:  • Ubuntu includes the very best in translations and accessibility
    376:  • Ubuntu is shipped in stable and regular release cycles; a new release will
    380:  • Ubuntu is entirely committed to the principles of open source software
    
    $ LANG=en_GB.UTF-8 grep -Pn '[\x80-\xFF]' install.en.txt| head
    9:Appendix F, GNU General Public License.
    330:when things go wrong. The Installation Howto can be found in Appendix A,
    337:Chapter 1. Welcome to Ubuntu
    359:1.1. What is Ubuntu?
    394:1.1.1. Sponsorship by Canonical
    402:1.2. What is Debian?
    456:1.2.1. Ubuntu and Debian
    461:1.2.1.1. Package selection
    475:1.2.1.2. Releases
    501:1.2.1.3. Development community
    
    • 6
  3. kos
    2016-02-07T23:16:46+08:002016-02-07T23:16:46+08:00

    这个 Perl 命令主要替换了那个grep命令(缺少的是颜色):

    perl -ne '/[\x80-\xFF]/&&print($ARGV."($.):\t^".$_)' *.xml
    
    • n: 导致 Perl 在你的程序周围假设以下循环,这使得它迭代文件名参数,有点像 sed -n 或 awk:

      LINE:
        while (<>) {
            ...             # your program goes here
        }
      
    • -e: 可用于输入一行程序。
    • /[\x80-\xFF]/&&print($ARGV."($.):\t^".$_):如果该行包含范围内的字符,则\x80-\xFF打印当前文件的名称、当前文件的行号、一个:\t^字符串和当前行的内容。

    包含问题中的示例文件和仅包含ààààà换行符的文件的示例目录上的输出:

    % perl -ne '/[\x80-\xFF]/&&print($ARGV."($.):\t^".$_)' file | head -n 10
    file(9):    ^Appendix F, GNU General Public License.
    file(14):   ^(codename "‘Xenial Xerus’"), for the 64-bit PC ("amd64") architecture. It also
    file(18):   ^â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”â”
    file(330):  ^when things go wrong. The Installation Howto can be found in Appendix A, 
    file(337):  ^Chapter 1. Welcome to Ubuntu
    file(359):  ^1.1. What is Ubuntu?
    file(368):  ^  • Ubuntu will always be free of charge, and there is no extra fee for the "
    file(372):  ^  • Ubuntu includes the very best in translations and accessibility
    file(376):  ^  • Ubuntu is shipped in stable and regular release cycles; a new release will
    file(380):  ^  • Ubuntu is entirely committed to the principles of open source software
    % perl -ne '/[\x80-\xFF]/&&print($ARGV."($.):\t^".$_)' file1
    file1(1):   ^ààààà
    
    • 3

相关问题

  • 如何从命令行仅安装安全更新?关于如何管理更新的一些提示

  • 如何从命令行刻录双层 dvd iso

  • 如何从命令行判断机器是否需要重新启动?

  • 文件权限如何工作?文件权限用户和组

  • 如何在 Vim 中启用全彩支持?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve