linux 命令 wc 输出格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5778476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
linux command wc output format
提问by user685275
I need to write a perl that mimic linux command wc
exactly(including and especially output format), while having realized the functionality, the output of wc
is really a headache, it seems that it is changing all the time, the following are a few examples(I will use □ to represent space character)
1. wc test_input_1
? □□705673□□4234032□26754553□test_input_1
2. wc test_input_2
? □14□□79□581□test_input_2
3. wc test_input_2 -w
? 79□test_input_2
4. wc test_input_2 -wc
? □79□581□test_input_2
5. cat test_input_2 | wc
? □□□□□14□□□□□□79□□□□□581
6. cat test_input_2 | wc -w
? 79
7. cat test_input_2 | wc -wc
? □□□□□79□□□□□581
我需要写一个wc
完全模仿linux命令的perl (包括特别是输出格式),虽然实现了功能,但是输出wc
真的很头疼,好像一直在变,下面是几个例子(我将用□来表示空格字符)
1. wc test_input_1
? □□705673□□4234032□26754553□test_input_1
2. wc test_input_2
? □14□□79□581□test_input_2
3. wc test_input_2 -w
? 79□test_input_2
4. wc test_input_2 -wc
? □79□581□test_input_2
5. cat test_input_2 | wc
? □□□□□14□□□□□□79□□□□□581
6. cat test_input_2 | wc -w
? 79
7. cat test_input_2 | wc -wc
?□□□□□79□□□□□581
anyone knows how wc formats its output? Any help is appreciated, thanks a lot.
有谁知道 wc 如何格式化它的输出?任何帮助表示赞赏,非常感谢。
采纳答案by Random832
The Open Group specification for wc
specifies the exact output format. The number of spaces where there is a space in the format string is unimportant (see File Format Notation).
Open Group 规范wc
指定了确切的输出格式。格式字符串中有空格的空格数并不重要(请参阅文件格式表示法)。
Historically, the numbers have often been formatted to occupy seven columns, excluding the space between them (some historic bad implementations have includedthe space in the seven columns, implemented in a way which made the output format ambiguous when the number of characters is more than six digits). Some of your output lines appear to be formatting based on the number of columns in the largest displayed number, others seem to be using seven columns.
从历史上看,数字通常被格式化为占据七列,不包括它们之间的空格(一些历史上的不良实现将空格包含在七列中,当字符数超过六位数)。您的某些输出行似乎根据最大显示数字中的列数进行格式化,而其他输出行似乎使用了七列。
回答by converter42
Start with
从...开始
$ info wc
The info page gives a brief description of output format. If that doesn't give you enough to work with, grab the coreutils sourceand read wc's source code.
信息页面给出了输出格式的简要说明。如果这还不够,请获取coreutils 源代码并阅读 wc 的源代码。
回答by rugk
If you came here (from a search engine) to find which numbers wc
prints out by default as you don't know how to interpret it's output and are annoyed all other answers just lead you to man/info pages and other resources you actually need to read, here is your quick Stackoverflow moment reliving you of your grief:
如果您来到这里(从搜索引擎)来查找wc
默认打印出的数字,因为您不知道如何解释它的输出并且对所有其他答案感到恼火,那么所有其他答案只会将您带到手册/信息页面和您实际需要的其他资源阅读,这是您在 Stackoverflow 上快速重温悲伤的时刻:
info wc
, as suggested by @converter42, explains it somewhere hidden:
info wc
,正如@converter42 所建议的,在隐藏的某个地方解释了它:
By default, ‘wc' prints three counts: the newline, words, and byte counts.
默认情况下,'wc' 打印三个计数:换行符、字数和字节数。
FYI also man wc
menttions it in a subtle way right at the beginning of the description:
仅供参考man wc
,在描述的开头也以一种微妙的方式提到了它:
Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified.
打印每个 FILE 的换行符、字和字节计数,如果指定了多个 FILE,则打印总行。
For example:
例如:
$ wc
abc def
xyz
[Ctrl+D] 2 3 12
= 2 (new)lines, 3 wordsand 12 bytes
= 2(新)行,3 个字和12 个字节