Linux wc-l command Statistics file a small line (typically Windows files)

Source: Internet
Author: User

Let's introduce briefly

WC (Word Count) The function of the command is to count the number of bytes, words, and lines in the specified file, and to display the output of the statistic results

format: WC file

Command parameters:

- C Statistics Bytes number ( number of bytes ) , and displays the file name

- L Count rows: Use line break ' \ n ' As the line end flag, which is actually a count of the number of newline characters

- M The number of statistics characters. This flag cannot be used with the- C flag.

- W Count the number of words. A word is defined as a string separated by a blank, a jump, or a newline character.

- L prints the length of the longest line.

-help Display Help information

--version Display version Information

Instance:

WC test.txt

6 132test.txt

Default output: line, word count, number of bytes

Test.txt content

Cat Test.txt

Test1 name1 Age1 sex1

Test2 name2 Age2 Sex2

Test3 Name3 Age3 sex3

Test4 name4 age4 sex4

Test5 name5 Age5 sex5

Test6 Name6 Age6 sex6

Problem: The number of WC rows is less than one line:

Because Wc–l is the count of rows by \ n As the line terminator, the last line, if not \ n, is counted as missing.

Examples: For example, in Windows generate the same above Test.txt The same file Testtt.txt, uploaded to Linux:

Cat Testtt.txt

Test1 name1 Age1 sex1

Test2 name2 Age2 Sex2

Test3 Name3 Age3 sex3

Test4 name4 age4 sex4

Test5 name5 Age5 sex5

Test6 name6 age6 sex6[[email protected] lmj]$

You can see the end is a bit strange. This is because the end of the file does not have \ n, but instead uses the file terminator EOF directly. This way, the file uses WC statistics for less than one line:

Wc-l Testtt.txt

5 136 Testtt.txt

You can't use pipelines either:

Cat Testtt.txt | Wc-l

5

Why is there no such problem with Linux?

Because the Vim editor automatically adds \ n at the end of the file, it adds the file terminator to EOF. (Linux text files are processed mainly, so vim will automatically add \ n for the last line)

and Dos2unix conversion to Windows files is not possible:

[Email protected] lmj]$ Dos2unix testtt.txt

dos2unix:converting file testtt.txt Tounix format ...

[Email protected] lmj]$ WC testtt.txt

5 24131 Testtt.txt

You can see that there is a compatibility problem with Windows files under Linux. The number of words in the file has not changed 24,byte 5 is the Windows downlink Terminator is carriage return \r+ \ n. And Linux is just a line change \ n

Vim binary can be seen differently, \ n displayed as., end of File no

Linux wc-l command Statistics file a small line (typically Windows files)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.