开发者

linux command wc output format

开发者 https://www.devze.com 2023-02-28 07:27 出处:网络
I need to write a perl that mimic linux command wc exactly(including and especially output format), while hav开发者_Go百科ing realized the functionality, the output of wc is really a headache, it seem

I need to write a perl that mimic linux command wc exactly(including and especially output format), while hav开发者_Go百科ing 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 -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

anyone knows how wc formats its output? Any help is appreciated, thanks a lot.


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.


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 source and read wc's source code.


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:

info wc, as suggested by @converter42, explains it somewhere hidden:

By default, ‘wc’ prints three counts: the newline, words, and byte counts.

FYI also man wc menttions it in a subtle way right at the beginning of the description:

Print newline, word, and byte counts for each FILE, and a total line if more than one FILE is specified.

For example:

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

= 2 (new)lines, 3 words and 12 bytes

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号