博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux中wc命令_Linux wc命令–文件中字,行,字符的计数
阅读量:2532 次
发布时间:2019-05-11

本文共 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”代表字数统计。

Linux wc命令 (Linux wc Command)

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命令。 可以通过键入以下命令来访问手册页。

Man Wc Page

Linux wc man page

Linux wc手册页

The following will be displayed as output. Let’s understand the wc help page part by part.

以下将显示为输出。 让我们逐步了解wc帮助页面。

Man Wc Output

Linux wc help output

Linux 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命令的语法 (Syntax of wc Command)

$ wc [OPTION]… [FILE]…

Linux wc命令示例 (Linux wc Command Example)

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命令并执行它,将得到以下输出。

Linux wc Command Output

Linux wc Command Output

Linux wc命令输出

The output has 3 lines corresponding to test.txt, test2.txt and total respectively.

输出有3行,分别对应于test.txt,test2.txt和total。

  1. The first column in the output shows the number of lines in the file. Since there is only a single line in both the text files shown above, the output is 1 for test.txt and test2.txt.

    输出的第一列显示文件中的行数。 由于上面显示的两个文本文件中只有一行,因此test.txt和test2.txt的输出为1。
  2. The second column in the output shows the number of words in the file, which in our case, is 12 and 15 respectively.

    输出的第二列显示文件中的单词数,在我们的示例中,分别为12和15。
  3. The third column in the output displays the number of bytes + 1 (or characters since each character occupies 1 byte in memory,) in the file, which in our case, is 58 and 74 respectively in test and test2. (The +1 extra byte is due to ‘\n’ at the end of the line automatically being added by the text editor).

    输出的第三列显示文件中的字节数+ 1(或字符,因为每个字符在内存中占1个字节),在本例中,在test和test2中分别为58和74。 (+1额外字节是由于文本编辑器自动在行尾添加了'\ n')。
  4. The total line simply displays the sum of lines, words, and bytes in all files mentioned in the input.

    总行仅显示输入中提到的所有文件中的行,单词和字节的总和。

Linux wc命令选项 (Linux wc Command Options)

The man page provides information about wc command options.

手册页提供了有关wc命令选项的信息。

Linux wc Command Options

Linux wc Command Options

Linux wc命令选项

The following options, as shown above, can be used with the command to print specific counts – only what is required by the user.

如上所示,以下选项可与命令一起使用以打印特定的计数-仅显示用户所需的计数。

  • -c, –bytes option can be used to print the number of bytes+1 in a file as shown below. If multiple files are inputted, an extra total line is also displayed in the output.
    Linux wc -c --bytes Option

    Linux wc -c –bytes Option

    -c,–bytes选项可用于打印文件中的字节数+1,如下所示。 如果输入了多个文件,则输出中还将显示一条额外的总行。

    Linux wc -c –bytes选项

  • -m, –chars option can be used to print only the number of characters+1 in a file, as shown below. Since the number of characters equals the number of bytes in this example, the output is the same for both -c and -m.

    -m,–chars选项只能用于打印文件中的字符数+1,如下所示。 由于在此示例中字符数等于字节数,因此-c和-m的输出相同。
  • -l or –lines option can be used to print only the number of lines in a file, as shown below. If multiple files are provided, an extra total line is also displayed in the output.

    -l或–lines选项可用于仅打印文件中的行数,如下所示。 如果提供了多个文件,则输出中还将显示额外的总计行。
  • Linux wc Lines Count

    Linux wc Lines Count

    Linux wc行数

  • -L or –max-line-length option can be used to print the number of characters in the line with the maximum number of characters of all lines present in the file.

    -L或–max-line-length选项可用于打印文件中存在的所有行的最大字符数的行中的字符数。
  • Linux Wc L Option

    Linux wc -L Option

    Linux wc -L选项

    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。

  • -w or –words can be used to print only the number of words in a file, as shown below.

    -w或–words只能用于打印文件中的单词数,如下所示。
  • Linux Wc Words Count

    Linux wc Words Count

    Linux wc字数统计

  • –files0-from=F can be used to read input from files which have their names contained within a file F, separated by null-characters (byte value 0) and not whitespaces/tabs/endline. In case there is no file or file name is “-“, the input is read from the standard input directly.

    –files0-from = F可用于从文件中读取输入,这些文件的名称包含在文件F中,并用空字符(字节值0)分隔,而不用空格/制表符/结尾。 如果没有文件或文件名是“-”,则直接从标准输入中读取输入。
  • Linux Wc Files From

    Linux wc Files From

    Linux wc文件从

    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即可退出该过程。

在cat和grep中使用wc命令 (Using wc Command with cat and grep)

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等其他命令一起使用非常有用。

Linux Wc With Cat

Linux wc With cat Command

Linux wc和cat命令

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的输出也可以这种方式显示。

Linux Wc With Grep

Linux wc With grep Command

带有grep命令的Linux wc

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”。

结论 (Conclusion)

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命令简单,易用,并且可以通过与catgrep等其他命令结合使用,从而从文件中搜索/过滤出有用的信息。 手动完成时,诸如查找大文本中包含某些字母的行数之类的任务可能很耗时。 此命令可在几秒钟内帮助解决此类问题。

翻译自:

linux中wc命令

转载地址:http://dumzd.baihongyu.com/

你可能感兴趣的文章
在线的JSON formate工具
查看>>
winform非常实用的程序退出方法!!!!!(转自博客园)
查看>>
xml解析
查看>>
centos安装vim
查看>>
linux工作调度(计划任务)
查看>>
hdu--1698 Just a Hook(线段树+区间更新+懒惰标记)
查看>>
Python学习笔记-EXCEL操作
查看>>
二月二——头牙诗词
查看>>
《吴忠与富平》之四:汉三水属国(北地属国、安定属国)
查看>>
丁酉立秋喜逢风雨
查看>>
vim删除#开头的行和正则表达式笔记
查看>>
python3 提成计算
查看>>
VBA赋值给指定单元格
查看>>
抽象类和接口总结回顾
查看>>
【语言处理与Python】5.3使用Python字典映射词及其属性
查看>>
设备信息
查看>>
Android Volley框架的使用(三)
查看>>
[错误总结] 控件frame设置无效
查看>>
Redis Java API
查看>>
oracle 查询表的定义语句
查看>>