Linux文本处理命令 -- WC

1、文件浏览

cat:查看文件内容

more:以翻页形式查看文件内容(只能向下翻页)

less:以翻页形式查看文件内容(可上下翻页)

head:默认查看文件的开始10行(或通过-n指定行数,即查看前n行)

tail:默认查看文件的结尾10行 (或通过-n指定行数,即查看末尾n行)

2、WC(1) : Words Counts 单词计数

NAME:  wc - print newline (换行符,新行) , word, and byte counts for each line

语法:wc [选项] 文件…

说明:该命令统计给定文件中的字节数字数行数。如果没有给出文件名,则从标准输入读取。

wc同时也给出所有指定文件的总统计数。字是由空格字符区分开的最大字符串。

Print newline, word, and byte counts for each FILE, anda total line if more than one FILEis specified.

With no FILE, or when FILE is -, read standard input.

该命令各选项含义如下:

   -c, --bytes :print the byte counts 统计字节数。即文件的大小

   -l, --lines :print the newline counts 统计行数

   -w , --words :print the word counts 统计单词(字)数。以空格分隔。

        -m, --chars :print the character counts 统计字符数。(包括字母、数字、空格与换行符)

                             一个英文字符1个字节,一个汉文2个字节

        -L, --max-line-length :print the length of the longest line 最长行的长度

这些选项可以组合使用。

[yanxia.dong@eslruntime07 exercise]$ 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 

输出列的顺序和数目不受选项的顺序的影响。总是按下述顺序显示并且每项最多一列。

行数、字数、字节数、文件名

如果命令行中没有文件名,则输出中不出现文件名。

例如:

[yanxia.dong@eslruntime07 exercise]$ ls  
[yanxia.dong@eslruntime07 exercise]$ touch myone  
[yanxia.dong@eslruntime07 exercise]$ echo "my first line" > myone   
[yanxia.dong@eslruntime07 exercise]$ cat myone   
my first line  
[yanxia.dong@eslruntime07 exercise]$ echo "my another first line" > mytwo  
[yanxia.dong@eslruntime07 exercise]$ cat mytwo   
my another first line  
[yanxia.dong@eslruntime07 exercise]$ wc -l myone mytwo   
 1 myone  
 1 mytwo  
 2 total  
[yanxia.dong@eslruntime07 exercise]$ wc -w myone mytwo   
 3 myone  
 4 mytwo  
 7 total  
[yanxia.dong@eslruntime07 exercise]$ wc -c myone mytwo   
14 myone  
22 mytwo  
36 total  
[yanxia.dong@eslruntime07 exercise]$ ls -al  
total 16  
drwxr-xr-x   2 yanxia.dong users 4096 Apr 16 12:43 .  
drwx------. 44 yanxia.dong users 4096 Apr 15 15:57 ..  
-rw-r--r--   1 yanxia.dong users   14 Apr 16 12:43 myone  
-rw-r--r--   1 yanxia.dong users   22 Apr 16 12:43 mytwo  
[yanxia.dong@eslruntime07 exercise]$ wc -m myone mytwo   
14 myone  
22 mytwo  
36 total
[yanxia.dong@eslruntime07 exercise]$ wc -mcwl myone mytwo
1 3 14 14 myone
1 4 22 22 mytwo
3 7 36 36 total

$ wc - lcw file1 file2
4 33 file1
7 52 file2
11 11 85 total

举例分析:

1、统计demo目录下,js文件数量:
find demo/ -name "*.js" | wc -l

[yanxia.dong@eslruntime07 exercise]$ touch main.c test.c  
[yanxia.dong@eslruntime07 exercise]$ find . -name "*.c" | wc -l  
2  
[yanxia.dong@eslruntime07 exercise]$ find . -name "*.c"  
./main.c  
./test.c
2、统计demo目录下所有js文件代码行数:
find demo/ -name "*.js" |xargs cat|wc -l或wc -l `find ./ -name "*.js"`|tail -n1
3、统计demo目录下所有js文件代码行数,过滤了空行:
find /demo -name "*.js" |xargs cat|grep -v ^$|wc -l
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值