6 Wc 命令计算文件中的行数、字数和字符数

厕所(缩写为字数统计)是Unix/Linux操作系统中的一个命令行工具,用于查找指定文件中的换行数、字数、字节数和字符数文件标准输出的参数并保存所有命名文件的总数。

当您定义文件参数,厕所命令打印文件名以及请求的计数。如果您没有定义文件名文件参数,它仅将总计数打印到标准输出。

在本文中,我们将讨论如何使用厕所命令通过实际示例计算文件的换行符、单词、字符或字节数。

wc 命令语法

的语法厕所命令如下所示。

# wc [options] filenames

以下是提供的选项和用法厕所命令。

  • wc -l– 打印文件中的行数。
  • wc -w– 打印文件中的字数。
  • wc -c– 显示文件中的字节数。
  • wc -m– 打印文件中的字符数。
  • wc -L– 仅打印文件中最长行的长度。

让我们看看如何使用 '厕所' 命令以及本文中的一些可用参数和示例。我们已经使用了'howtoing.txt' 用于测试命令的文件。

我们来看看输出结果howtoing.txt文件使用如下图所示。

$ cat tecmint.txt

Red Hat
CentOS
AlmaLinux
Rocky Linux
Fedora
Debian
Scientific Linux
OpenSuse
Ubuntu
Xubuntu
Linux Mint
Deepin Linux
Slackware
Mandriva

1. WC命令的基本示例

这 '厕所' 不传递任何参数的命令将显示 ' 的基本结果howtoing.txt' 文件。下面显示的三个数字分别是12(行数),16(字数), 和112(字节数)的文件。

$ wc tecmint.txt

12  16 112 tecmint.txt

2. 计算文件中的行数

使用选项 ' 计算文件中换行符的数量-l',它打印给定文件的行数。比如说,以下命令将显示文件中换行符的计数。

在输出中,第一个字段指定为计数,第二个字段指定为文件名。

$ wc -l tecmint.txt

12 tecmint.txt

3. 计算文件中的单词数

-w争论与厕所命令打印文件中的字数。键入以下命令来计算文件中的单词数。

$ wc -w tecmint.txt

16 tecmint.txt

4. 计算文件中的字符数

使用选项时-m厕所命令将打印总数字符数在一个文件中。

$ wc -m tecmint.txt

112 tecmint.txt

5. 计算文件中的字节数

使用选项时-c将打印文件的字节数。

$ wc -c tecmint.txt

112 tecmint.txt

6. 显示文件中最长行的长度

这 '厕所'命令允许参数'-L',它可用于打印出最长的长度(字符数) 文件中的行。

所以,我们有最长的字符行('科学Linux')在一个文件中。

$ wc -L tecmint.txt

16 tecmint.txt

7. 检查 wc 命令选项

有关的更多信息和帮助厕所命令,只需运行 'wc --help' 或者 'man wc' 从命令行。

$ wc --help
OR
$ man wc

wc 命令用法

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.  A word is a non-zero-length sequence of
characters delimited by white space.

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

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 maximum display width
  -w, --words            print the word counts
      --help     display this help and exit
      --version  output version information and exit

GNU coreutils online help: <https://www.gnu.org/software/coreutils/>
Full documentation at: <https://www.gnu.org/software/coreutils/wc>
or available locally via: info '(coreutils) wc invocation'

在本文中,您了解了厕所命令,这是一个简单的命令行实用程序,用于计算文本文件中的行数、单词数、字符数和轮空数。有很多这样的,您应该学习并掌握命令行技能。