6 WC命令计算文件中的行,单词和字符数

厕所(缩写字数)是UNIX/Linux操作系统中的命令行工具,用于查找Newline计数,单词计数,字节和字符计数的数量文件对标准输出的参数并保留所有命名文件的总数。

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

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

WC命令语法

语法厕所命令如下所示。

# wc [options] filenames

以下是由厕所命令。

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

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

让我们找出输出tecmint.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命令的基本示例

这 '厕所'命令而无需传递任何参数将显示'的基本结果tecmint.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'

在本文中,您已经了解了厕所命令,这是一个简单的命令行实用程序,可计算文本文件中的行,单词,字符和旁边的数量。有很多这样的其他Linux命令,您应该学习并掌握您的命令行技能。