Shell Script WC command Detailed!! Demand output Results

Source: Internet
Author: User

The WC command is used to print the number of lines of text, number of words, number of bytes, and so on (print the newlines, words, and bytes in files). In Windows Word has a "word count" tool, you can help us to the selected range of words, characters count out. The WC command under Linux can implement this function. When using VI to open a file, the information below will also show the number of rows and bytes.

Common parameters


Format: Wc-l <file>

Prints the number of lines of text for the specified file. (l= lowercase L)

The following parameters can be used in combination.

Parameters:-C,--bytes

Number of bytes printed (print the byte counts)


Parameters:-M,--chars

Number of characters printed (print the character counts)


Parameter:-L,--lines

Print the number of lines (print the newline counts)


Parameter:-L,--max-line-length

Print the length of the longest line (print the length of the longest lines)


Parameters:-W,--words

Print the number of words (print the word counts)

Using the example


Example One


[Email protected] ~]# WC/ETC/PASSWD

2027/etc/passwd

Number of Rows Word count bytes file name

[Email protected] ~]#

[Email protected] ~]# wc-l/etc/passwd

46/etc/passwd

[Email protected] ~]# WC-CMLWL/ETC/PASSWD

2027 2027 74/ETC/PASSWD

[Email protected] ~]# WC-CMLLW/ETC/PASSWD

2027 2027 74/ETC/PASSWD

[Email protected] ~]# WC-WCMLL/ETC/PASSWD

2027 2027 74/ETC/PASSWD

[Email protected] ~]#

Here's the problem: from the command line above, the order of the WC output data seems to have nothing to do with the order of several parameters.

Example two with WC command How do I print statistics without printing file names


Use pipe lines. This is especially useful when writing shell scripts.

[Email protected] ~]# wc-l/etc/passwd

46/etc/passwd

[Email protected] ~]# CAT/ETC/PASSWD | Wc-l

46

[Email protected] ~]#


An example of three Chinese coding problems


The execution environment is encoded in Chinese.

[Email protected] ~]# echo $LANG

Zh_cn. GB18030

Chinese encoded file Ehr_object.gv,utf8 encoded file EHR_OBJECT_UTF8.GV.

[Email protected] ~]# file EHR_OBJECT.GV EHR_OBJECT_UTF8.GV

ehr_object.gv:iso-8859 text

Ehr_object_utf8.gv:utf-8 Unicode Text

[Email protected] ~]#

[Email protected] ~]# WC EHR_OBJECT.GV EHR_OBJECT_UTF8.GV

830 EHR_OBJECT.GV

Wc:ehr_object_utf8.gv:4: invalid or incomplete multibyte character or wide character

866 EHR_OBJECT_UTF8.GV

22 210 1696 Total

[Email protected] ~]#

Example four Chinese word count calculation


[email protected] ~]# cat Test.txt

Hello Word

Linux


[Email protected] ~]# WC test.txt

3 2 Test.txt

Number of Rows Word count bytes file name





Example four print out the order according to the requirements change

WC-LC test.txt |awk ' {print $ "" $ $ "" $ ">> test2.txt


Shell Script WC command Detailed!! Demand output result

Related Article

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.