Unix wc command

Source: Internet
Author: User

Address: http://cicikasa.blog.163.com/blog/static/13757535520101264115249/

 

The WC command of Linux is usually used together with other commands to calculate the line and other information.
In Linux, WC is used for counting. The number of lines, characters, and bytes of the returned file.

Let's look at an example:

WC wc1.txt
3 5 16 wc1.txt



The output information is the number of lines, the number of characters, and the name of the file. The specific point is a single statistic.

WC-M filename: displays the number of characters in a file.
WC-l filename: displays the number of lines in a file.
WC-l filename: displays the length of the longest row in a file.
WC-W filename: displays the number of words in a file.

Note: It seems that the lines in WC statistics are determined by line breaks. That is to say, the last line must have a line break.
In the end, the number of WC rows is correct. Otherwise, one row is missing.

To illustrate this problem, let's look at a Perl test:

Perl-e 'print "A" '| WC
0 1 1
Perl-e 'print "A/N" '| WC
1 1 2

In the preceding example, the print command prints a out. If the line break/N is not included, the line does not exist.

There are several notes:
1: Three Chinese Characters
Bytes (unverified, personally considered as two bytes
), A carriage return and other invisible characters also occupy one byte
2: If there is no carriage return at the end of a row, it is not considered a line. That is to say, if there is no line break at the end of the last line of a file, the number of lines counted by the WC command will be one fewer than the actual number of lines, the actual number of rows is the number of rows you see. In fact, if there is no carriage return, it cannot be counted as a line (Note: if it is in an existing text, by default, all rows have an invisible carriage return)

3: A word is a continuous character, that is, a continuous letter or Chinese character that is not separated by null characters is counted as a word.

For example, a text named abc.txt is shown below, which is opened by VI: (for details, we use the arrow "carriage return"
Is not actually found)

 

You are nice.

Right ??

 

 

OK.

 

~

~

~

You are nice.
Bytes

Right ??
Bytes

Bytes

Bytes

OK.

Bytes

~

~

~

Then:

WC abc.txt

6 5 28 abc.txt

Six lines, five words, and 28 characters. Let's count it.

There are 6 lines including invisible carriage returns;

There are five consecutive letters separated by no-null characters:You

,Are

,Nice.

,Right ??

,OK.

Bytes (or characters) contain a total of 28 carriage returns.

 

Example:

Example1: how to count the number of files in a directory and the total number of lines of code in Linux


Command to know the total number of objects with the specified Suffix:
Find.-Name "*. cpp" | WC-l


Know the total number of lines of code in a directory and the number of lines of a single file:
Find.-Name "*. H" | xargs WC-l

Example2: count the number of files in the folder in Linux

Method 1:
Ls-L | grep "^-" | WC-l

The LS-l long list outputs the file information under this directory (note that the file here may be a directory, link, or device file, different from the common file ). If LS-LR | grep "^-" | WC-L is used, the files in the subdirectories can be counted together.

Grep ^-here, a part of the output information of the long list is filtered out, and only the general files are retained. If only the directory is retained, it is ^ d.

WC-l counts the number of rows of output information. Because only common files are available, the statistical result is the number of rows of general files, that is, the number of files.

Method 2:
Find./-type F | WC-l

By default, find will go to the subdirectory to search for files in the current directory.Find./-maxdepth 1-type F | WC-l
You can.

Note that the second method is much faster than the first method, especially when the sub-directories are also counted.
Example3: How many users have logged on to the system?

Who | WC-l

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.