美文网首页
shell 命令 wc 的使用简介

shell 命令 wc 的使用简介

作者: Jamza | 来源:发表于2021-08-26 12:40 被阅读0次

简介

wc 命令可以帮助我们计算文件的字节数、字数、或者文件的行数,常用于统计文件的相关信息。

wc 命令的 帮助信息比较简短:

[root@A23488811 test]# 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.  A word is a non-zero-length sequence of characters
delimited by white space.
The options below may be used to select which counts are printed, always in
the following order: newline, word, character, byte, maximum line length.
  -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

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
For complete documentation, run: info coreutils 'wc invocation'

参数选项

支持的主要参数选项包括:

  1. -c:统计字节数
  2. -m:统计字符数,该参数不能与 -c 一起使用
  3. -l:统计行数
  4. -w:统计字数,这里的字指的是由空格、换行符等分隔的字符串

示例

使用以下的文件作为测试文件:

[root@A23488811 test]# cat testfile
Linux networks are becoming more and more common, but scurity is often an overlooked
issue. Unfortunately, in today’s environment all networks are potential hacker targets,
fro0m tp-secret military research networks to small home LANs.
Linux Network Securty focuses on securing Linux in a networked environment, where the
security of the entire network needs to be considered rather than just isolated machines.
It uses a mix of theory and practicl techniques to teach administrators how to install and
use security applications, as well as how the applcations work and why they are necesary.
[root@A23488811 test]#

对测试文件直接使用 wc 命令:

[root@A23488811 test]# wc testfile
  7  92 608 testfile

7 表示测试文件的行数,92 表示测试文件的字数,608 表示测试文件的字节数:

[root@A23488811 test]# wc -l testfile
7 testfile
[root@A23488811 test]# wc -w testfile
92 testfile
[root@A23488811 test]# wc -c testfile
608 testfile

可以通过命令输出的重定向功能,使得 wc 命令获得更广泛的使用方式,比如

[root@A23488811 test]# ll
total 16
drwxr-xr-x 2 root root   6 May 31 09:37 test
-rw-r--r-- 1 root root 608 May 31 09:29 testfile
-rw-r--r-- 1 root root 608 May 31 09:37 testfile1
-rw-r--r-- 1 root root 608 May 31 09:37 testfile2
-rw-r--r-- 1 root root 608 May 31 09:37 testfile3
[root@A23488811 test]#
[root@A23488811 test]#
[root@A23488811 test]# ll | grep "^-" | wc -l
4

以上命令可以统计当前目录下具有多少个普通文件。其中 grep "^-" 表示获取以 - 开头的行,这些行表示的都是普通文件,通过命令输出的重定向,作为 wc 命令的输入,可统计出当前目录下普通文件的数量。

相关文章

  • linux shell基础(三)

    8.10 shell特殊符号cut命令8.11 sort_wc_uniq命令8.12 tee_tr_split命令...

  • shell特殊符_cut命令、sort_wc_uniq命令、te

    810 shell特殊符_cut命令 image.png 管道相关的命令 811 sort_wc_uniq命令 8...

  • wc

    wc 命令 命令简介: 简单小巧的计数工具,wc命令用于统计并输出一个文件中行、单词和字节的数目.

  • linux shell wc命令

    wc命令的功能为统计指定文件中的字节数、字数、行数,并将统计结果显示输出。 语法:wc[选项]文件… 说明:该命令...

  • mac上的终端bash命令

    (一) Bourne-Again Shell简介 mac上的终端使用的是Bourne-Again Shell命令,...

  • Linux shell基础知识(三)

    摘要: 8.10 shell特殊符号cut命令 8.11 sort_wc_uniq命令 8.12 tee_tr_s...

  • 基础-25、linux shell基础知识(三)

    笔记内容:8.10 shell特殊符号cut命令8.11 sort_wc_uniq命令8.12 tee_tr_sp...

  • Linux命令学习之:wc命令

    Linux命令学习之:wc命令 统计文件里面有多少单词,多少行,多少字符。 wc语法 默认使用wc统计/etc/p...

  • 2018-10-19

    10月15日任务 8.10 shell特殊符号cut命令 8.11 sort_wc_uniq命令 8.12 tee...

  • Linux命令--wc

    一、 命令简介:   wc命令用来计算数字。利用wc指令我们可以计算文件的Byte数、字数或是列数,并将统计结...

网友评论

      本文标题:shell 命令 wc 的使用简介

      本文链接:https://www.haomeiwen.com/subject/ahqmbltx.html