本文共 7340 字,大约阅读时间需要 24 分钟。
linux中wc命令
Linux wc command is used to count the number of words, lines, and characters in a file. The ‘wc’ stands for Word Count.
Linux wc命令用于计算文件中单词,行和字符的数量。 “ wc”代表字数统计。
Let’s see how the wc command is defined by the man page. The man page can be accessed by typing in the following command.
让我们看看手册页如何定义wc命令。 可以通过键入以下命令来访问手册页。
The following will be displayed as output. Let’s understand the wc help page part by part.
以下将显示为输出。 让我们逐步了解wc帮助页面。
The wc command as described can be used to get the number of newlines, words or bytes contained in a file specified.
所描述的wc命令可用于获取指定文件中包含的换行符,单词或字节的数量。
The output will contain the number of newlines, words or bytes (file-wise, in case multiple files are inputted) followed by a “total” line in the end which will show the total sum of the words, newlines or bytes in all files.
输出将包含换行符,单词或字节的数量(如果输入多个文件,则为文件方式),最后是“总计”行,该行将显示所有文件中单词,换行符或字节的总和。 。
$ wc [OPTION]… [FILE]…
Let’s see an example to understand wc command.
让我们看一个了解wc命令的示例。
I have created 2 sample text files – test.txt and test2.txt on Desktop and navigated to the Desktop directory using the cd command.
我已经在桌面上创建了2个示例文本文件– test.txt和test2.txt,并使用cd命令导航到Desktop目录。
Now, typing in the wc command according to the syntax and executing it, gives the following output.
现在,根据语法键入wc命令并执行它,将得到以下输出。
The output has 3 lines corresponding to test.txt, test2.txt and total respectively.
输出有3行,分别对应于test.txt,test2.txt和total。
The man page provides information about wc command options.
手册页提供了有关wc命令选项的信息。
The following options, as shown above, can be used with the command to print specific counts – only what is required by the user.
如上所示,以下选项可与命令一起使用以打印特定的计数-仅显示用户所需的计数。
Here, I have created 2 new .txt files – file1.txt and file2.txt containing names of some fruits and car companies respectively.
在这里,我创建了2个新的.txt文件– file1.txt和file2.txt,分别包含一些水果和汽车公司的名称。
We can see that the output shows 10 for file1.txt and 13 for file2.txt. The lines “Watermelon” and “Maruti Suzuki” are the longest in their respective files and have 10 and 13 characters respectively.
我们可以看到输出显示file1.txt为10,file2.txt为13。 “西瓜”和“丸木铃木”行在各自的文件中最长,分别具有10和13个字符。
You might notice that the total line seems to be displaying an incorrect sum. This is because when using -L, total displays the number largest among those displayed above it as output, which in our case is 13.
您可能会注意到总行似乎显示了不正确的总和。 这是因为使用-L时,total显示在其上方显示的最大数字作为输出,在本例中为13。
When – is written as file name and the command is executed, terminal waits for an input. If we type the file name then and press Ctrl+D twice, the output is displayed in the same line as shown. Once no more inputs are required, Ctrl+C is pressed to exit the process.
当将–写入文件名并执行命令时,终端等待输入。 如果我们输入文件名,然后按两次Ctrl + D,则输出将显示在同一行,如图所示。 一旦不需要更多输入,按Ctrl + C即可退出该过程。
Linux wc command can also take inputs directly from the standard input-output of another command can be redirected as input to wc. This is very useful for using it with other commands like cat and grep.
Linux wc命令还可以直接从另一个命令的标准输入输出中获取输入,而另一个命令可以作为输入重定向到wc。 这对于将其与cat和grep等其他命令一起使用非常有用。
Here, the output from the cat was piped (redirected) from stdout to stdin of wc command. Output for test and test2 can also be shown in this way.
在这里,cat的输出从stdout通过管道传递(重定向)到wc命令的stdin。 test和test2的输出也可以这种方式显示。
In this example, our goal is to find the total number of lines in file1.txt containing the letter “e”. For this, grep is used as shown, to filter out the letter “e” from the file. Then the output is piped (redirected) to the standard input of wc. We have added the -l option to wc in order to find the number of lines in the input.
在此示例中,我们的目标是在file1.txt中找到包含字母“ e”的总行数。 为此,使用grep如图所示,以从文件中过滤出字母“ e”。 然后,将输出通过管道传输(重定向)到wc的标准输入。 我们在wc中添加了-l选项,以查找输入中的行数。
Hence, our output comes out to be 4 as all names of fruits contain the letter “e” except Kiwi in the last line.
因此,我们的输出为4,因为除了最后一行中的奇异果,所有水果的名称都包含字母“ e”。
Linux wc command is simple, easy to use and can be used to search/filter out useful information from files by combining it with other commands like cat
and grep
. Tasks like finding number of lines containing certain alphabets in a huge text can be time-consuming when done manually. This command helps solve such problems in a matter of seconds.
Linux wc命令简单,易用,并且可以通过与cat
和grep
等其他命令结合使用,从而从文件中搜索/过滤出有用的信息。 手动完成时,诸如查找大文本中包含某些字母的行数之类的任务可能很耗时。 此命令可在几秒钟内帮助解决此类问题。
翻译自:
linux中wc命令
转载地址:http://dumzd.baihongyu.com/