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