wc命令用来打印文件的文本行数、单词数、字节数等(print the number of newlines, words, and  bytes in  files)。在Windows的Word中有个“字数统计”的工具,可以帮我们把选中范围的字数、字符数统计出来。Linux下的wc命令可以实现这个 功能。使用vi打开文件的时候,底下的信息也会显示行数和字节数。

常用参数

格式:wc -l <file>

打印指定文件的文本行数。(l=小写L)

以下参数可组合使用。

参数:-c, --bytes[喝小酒的网摘]http://blog.hehehehehe.cn/a/17301.htm
打印字节数(print the byte counts)

参数:-m, --chars
打印字符数(print the character counts)

参数:-l, --lines
打印行数(print the newline counts)

参数:-L, --max-line-length
打印最长行的长度(print the length of the longest line)

参数:-w, --words
打印单词数(print the word counts)

使用示例

示例一

[root@jfht ~]# wc /etc/passwd
  46   66 2027 /etc/passwd

行数 单词数 字节数 文件名
[root@jfht ~]#

[root@jfht ~]# wc -l /etc/passwd
46 /etc/passwd
[root@jfht ~]# wc -cmlwL /etc/passwd
  46   66 2027 2027   74 /etc/passwd
[root@jfht ~]# wc -cmlLw /etc/passwd
  46   66 2027 2027   74 /etc/passwd
[root@jfht ~]# wc -wcmlL /etc/passwd  
  46   66 2027 2027   74 /etc/passwd
[root@jfht ~]#

问题来了:从上面的命令行执行结果来看,wc的输出数据的顺序与的几个参数的顺序好像没有关系?!

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

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

[root@jfht ~]# wc -l /etc/passwd
46 /etc/passwd
[root@jfht ~]# cat /etc/passwd | wc -l
46
[root@jfht ~]#


示例三 中文编码的问题

执行环境是中文编码的。

[root@jfht ~]# echo $LANG
zh_CN.GB18030

中文编码文件ehr_object.gv,UTF8编码的文件ehr_object_utf8.gv。

[root@jfht ~]# file ehr_object.gv ehr_object_utf8.gv
ehr_object.gv:      ISO-8859 text
ehr_object_utf8.gv: UTF-8 Unicode text
[root@jfht ~]#

[root@jfht ~]# wc ehr_object.gv ehr_object_utf8.gv
  11  105  830 ehr_object.gv
wc: ehr_object_utf8.gv:4: 无效或不完整的多字节字符或宽字符
  11  105  866 ehr_object_utf8.gv
  22  210 1696 总计
[root@jfht ~]#

示例四 中文单词数的计算

[root@jfht ~]# cat test.txt
你好Word
Linux

[root@jfht ~]# wc test.txt  
 3  2 16 test.txt

行数 单词数 字节数 文件名
[root@jfht ~]#



grep常用用法

linux基础命令(6)_linux

[root@www ~]# grep [-acinv] [--color=auto] '搜寻字符串' filename
选项与参数:-a :将 binary 文件以 text 文件的方式搜寻数据-c :计算找到 '搜寻字符串' 的次数-i :忽略大小写的不同,所以大小写视为相同-n :顺便输出行号-v :反向选择,亦即显示出没有 '搜寻字符串' 内容的那一行!--color=auto :可以将找到的关键词部分加上颜色的显示喔!

linux基础命令(6)_linux

 

将/etc/passwd,有出现 root 的行取出来

linux基础命令(6)_linux

# grep root /etc/passwdroot:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
或# cat /etc/passwd | grep root 
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

linux基础命令(6)_linux

 

将/etc/passwd,有出现 root 的行取出来,同时显示这些行在/etc/passwd的行号

# grep -n root /etc/passwd1:root:x:0:0:root:/root:/bin/bash30:operator:x:11:0:operator:/root:/sbin/nologin

 在关键字的显示方面,grep 可以使用 --color=auto 来将关键字部分使用颜色显示。 这可是个很不错的功能啊!但是如果每次使用 grep 都得要自行加上 --color=auto 又显的很麻烦~ 此时那个好用的 alias 就得来处理一下啦!你可以在 ~/.bashrc 内加上这行:『alias grep='grep --color=auto'』再以『 source ~/.bashrc 』来立即生效即可喔! 这样每次运行 grep 他都会自动帮你加上颜色显示啦

 

将/etc/passwd,将没有出现 root 的行取出来

# grep -v root /etc/passwdroot:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

 

将/etc/passwd,将没有出现 root 和nologin的行取出来

# grep -v root /etc/passwd | grep -v nologin
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

 

用 dmesg 列出核心信息,再以 grep 找出内含 eth 那行,要将捉到的关键字显色,且加上行号来表示:

[root@www ~]# dmesg | grep -n --color=auto 'eth'247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10248:eth0: Identified 8139 chip type 'RTL-8139C'294:eth0: link up, 100Mbps, full-duplex, lpa 0xC5E1305:eth0: no IPv6 routers present
# 你会发现除了 eth 会有特殊颜色来表示之外,最前面还有行号喔!

在关键字的显示方面,grep 可以使用 --color=auto 来将关键字部分使用颜色显示。 这可是个很不错的功能啊!但是如果每次使用 grep 都得要自行加上 --color=auto 又显的很麻烦~ 此时那个好用的 alias 就得来处理一下啦!你可以在 ~/.bashrc 内加上这行:『alias grep='grep --color=auto'』再以『 source ~/.bashrc 』来立即生效即可喔! 这样每次运行 grep 他都会自动帮你加上颜色显示啦

 

用 dmesg 列出核心信息,再以 grep 找出内含 eth 那行,在关键字所在行的前两行与后三行也一起捉出来显示

linux基础命令(6)_linux

[root@www ~]# dmesg | grep -n -A3 -B2 --color=auto 'eth'245-PCI: setting IRQ 10 as level-triggered246-ACPI: PCI Interrupt 0000:00:0e.0[A] -> Link [LNKB] ...247:eth0: RealTek RTL8139 at 0xee846000, 00:90:cc:a6:34:84, IRQ 10248:eth0: Identified 8139 chip type 'RTL-8139C'249-input: PC Speaker as /class/input/input2250-ACPI: PCI Interrupt 0000:00:01.4[B] -> Link [LNKB] ...251-hdb: ATAPI 48X DVD-ROM DVD-R-RAM CD-R/RW drive, 2048kB Cache, UDMA(66)
# 如上所示,你会发现关键字 247 所在的前两行及 248 后三行也都被显示出来!
# 这样可以让你将关键字前后数据捉出来进行分析啦!

linux基础命令(6)_linux

 

根据文件内容递归查找目录

# grep ‘energywise’ *           #在当前目录搜索带'energywise'行的文件

# grep -r ‘energywise’ *        #在当前目录及其子目录下搜索'energywise'行的文件
# grep -l -r ‘energywise’ *     #在当前目录及其子目录下搜索'energywise'行的文件,但是不显示匹配的行,只显示匹配的文件

这几个命令很使用,是查找文件的利器。