2

我需要写一个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 -w79□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 -w79
7. cat test_input_2 | wc -wc□□□□□79□□□□□581

有谁知道 wc 如何格式化其输出?任何帮助表示赞赏,非常感谢。

4

3 回答 3

1

从...开始

$ info wc

信息页面提供了输出格式的简要说明。如果这还不够,请获取coreutils 源代码并阅读 wc 的源代码。

于 2011-04-25T13:07:00.210 回答
1

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).

Historically, the numbers have often been formatted to occupy seven columns, excluding the space between them (some historic bad implementations have included the 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.

于 2011-04-25T13:20:51.593 回答
1

如果您来到这里(来自搜索引擎)查找wc默认打印出的数字,因为您不知道如何解释它的输出并且对所有其他答案感到恼火,只会将您带到手册/信息页面和您实际需要的其他资源阅读,这是您快速的 Stackoverflow 时刻,让您重温悲伤:

info wc,正如@converter42 所建议的,在某个隐藏的地方解释它:

默认情况下,'wc' 打印三个计数:换行符、单词和字节计数。

FYI 还在描述man wc的开头以一种微妙的方式提到了它:

打印每个 FILE 的换行符、字数和字节数,如果指定了多个 FILE,则打印总行数。

例如:

$ wc
abc def
xyz
[Ctrl+D]      2       3      12

= 2(新)行3 个字12 个字节

于 2020-05-25T15:11:20.967 回答