您的位置:首页 > 运维架构 > Linux

linux基础命令之:Linux统计命令wc学习

2016-02-17 17:46 645 查看
Linux系统中的wc(Word Count)命令的功能为统计指定文件中的字节数、字数、行数,并将统计结果显示输出。

1、wc命令帮助

[spark@Master etl-script]$ wc --help
Usage: wc [OPTION]... [FILE]...
or:  wc [OPTION]... --files0-from=F
Print newline, word, and byte counts for each FILE, and a total line if
more than one FILE is specified.  With no FILE, or when FILE is -,
read standard input.
-c, --bytes            print the byte counts
-m, --chars            print the character counts
-l, --lines            print the newline counts
--files0-from=F    read input from the files specified by
NUL-terminated names in file F;
If F is - then read names from standard input
-L, --max-line-length  print the length of the longest line
-w, --words            print the word counts
--help     display this help and exit
--version  output version information and exit

Report wc bugs to bug-coreutils@gnu.org
GNU coreutils home page: <http://www.gnu.org/software/coreutils/>
General help using GNU software: <http://www.gnu.org/gethelp/>
For complete documentation, run: info coreutils 'wc invocation'


2、中文命令参数说明

统计指定文件中的字节数、字数、行数,并将统计结果显示输出。该命令统计指定文件中的字节数、字数、行数。如果没有给出文件名,则从标准输入读取。wc同时也给出所指定文件的总统计数。

-c 统计字节数。

-l 统计行数。

-m 统计字符数。这个标志不能与 -c 标志一起使用。

-w 统计字数。一个字被定义为由空白、跳格或换行字符分隔的字符串。

-L 打印最长行的长度。

-help 显示帮助信息

--version 显示版本信息

3、使用实例

实例1:
查看文件的字节数、字数、行数

命令:

wc test.txt

实例2:用wc命令怎么做到只打印统计数字不打印文件名

命令:

输出:

[root@localhost test]# wc -l test.txt

7 test.txt

[root@localhost test]# cat test.txt |wc -l

7

说明:使用管道线,这在编写shell脚本时特别有用。

实例3:用来统计当前目录下的文件数

命令:

ls -l | wc -l

输出:

[root@localhost test6]# ls -l | wc -l

8

说明:数量中包含当前目录

实例4:

$ wc - lcw file1 file2

4 33 file1

7 52 file2

11 11 85 total

省略任选项-lcw,wc命令的执行结果与上面一样

实例5:

wc命令用来计算一个文件或者指定的多个文件中的行数,单词数和字符数。如:

wc filename

第一列显示行数,第二列显示单词数,第三列显示字符数。

wc 有四个参数可选,分别是l,c,m,w

wc -l filename 报告行数

wc -c filename 报告字节数

wc -m filename 报告字符数

wc -w filename 报告单词数
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: