Linux查看文件的行数,字数,字节数

简介: Linux查看文件的行数,字数,字节数

介绍

在Linux系统中这统计非常方便,只需要简单的几个命令就可以搞定,这个命令就是 wc

wc --help
用法:wc [选项]... [文件]...
 或:wc [选项]... --files0-from=F
输出每个指定文件的行数、单词计数和字节数,如果指定了
多于一个文件,继续给出所有相关数据的总计。如果没有指定
文件,或者文件为"-",则从标准输入读取数据。
  -c, --bytes        输出字节数统计
  -m, --chars        输出字符数统计
  -l, --lines        输出行数统计
      --files0-from=文件    从指定文件读取以NUL 终止的名称,如果该文件被
                    指定为"-"则从标准输入读文件名
  -L, --max-line-length    显示最长行的长度
  -w, --words            显示单词计数
      --help        显示此帮助信息并退出
      --version        显示版本信息并退出

使用实例

  • 获取文件行数

wc -l app.log
30000 app.log
  • 获取文件单词数
wc -w app.log
30000 app.log
  • 获取文件字节数
wc -c app.log
3000 app.log

grep

查询文件的行数或字数只是个简单的需求场景,有时候我们其实是要获取多少匹配关键字的行数,那么这种情况如何实现呢,这种情况我们需要使用另外一个grep命令来配置wc来完成我们的需求场景。

lennlouis@dpdk-vm:~$ grep --help
Usage: grep [OPTION]... PATTERNS [FILE]...
Search for PATTERNS in each FILE.
Example: grep -i 'hello world' menu.h main.c
PATTERNS can contain multiple patterns separated by newlines.
Pattern selection and interpretation:
  -E, --extended-regexp     PATTERNS are extended regular expressions
  -F, --fixed-strings       PATTERNS are strings
  -G, --basic-regexp        PATTERNS are basic regular expressions
  -P, --perl-regexp         PATTERNS are Perl regular expressions
  -e, --regexp=PATTERNS     use PATTERNS for matching
  -f, --file=FILE           take PATTERNS from FILE
  -i, --ignore-case         ignore case distinctions in patterns and data
      --no-ignore-case      do not ignore case distinctions (default)
  -w, --word-regexp         match only whole words
  -x, --line-regexp         match only whole lines
  -z, --null-data           a data line ends in 0 byte, not newline
Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             display version information and exit
      --help                display this help text and exit
Output control:
  -m, --max-count=NUM       stop after NUM selected lines
  -b, --byte-offset         print the byte offset with output lines
  -n, --line-number         print line number with output lines
      --line-buffered       flush output on every line
  -H, --with-filename       print file name with output lines
  -h, --no-filename         suppress the file name prefix on output
      --label=LABEL         use LABEL as the standard input file name prefix
  -o, --only-matching       show only nonempty parts of lines that match
  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is 'binary', 'text', or 'without-match'
  -a, --text                equivalent to --binary-files=text
  -I                        equivalent to --binary-files=without-match
  -d, --directories=ACTION  how to handle directories;
                            ACTION is 'read', 'recurse', or 'skip'
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                            ACTION is 'read' or 'skip'
  -r, --recursive           like --directories=recurse
  -R, --dereference-recursive  likewise, but follow all symlinks
      --include=GLOB        search only files that match GLOB (a file pattern)
      --exclude=GLOB        skip files that match GLOB
      --exclude-from=FILE   skip files that match any file pattern from FILE
      --exclude-dir=GLOB    skip directories that match GLOB
  -L, --files-without-match  print only names of FILEs with no selected lines
  -l, --files-with-matches  print only names of FILEs with selected lines
  -c, --count               print only a count of selected lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name
Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is 'always', 'never', or 'auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS/Windows)
When FILE is '-', read standard input.  With no FILE, read '.' if
recursive, '-' otherwise.  With fewer than two FILEs, assume -h.
Exit status is 0 if any line (or file if -L) is selected, 1 otherwise;
if any error occurs and -q is not given, the exit status is 2.
Report bugs to: bug-grep@gnu.org
GNU grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <https://www.gnu.org/gethelp/>
  • 查询文件中单词的行数
grep 'hello' app.log | wc -l


相关文章
|
11天前
|
Linux 数据安全/隐私保护 Windows
命令方式:window向linux传文件
【10月更文挑战第6天】本文介绍了如何在Linux系统中通过命令`ip a`获取IP地址,并在Windows系统下使用CMD命令行工具和SCP命令实现文件传输。示例展示了如何将D盘中的`mm.jar`文件上传至IP地址为192.168.163.122的Linux系统的/up/目录下,最后在Linux系统中确认文件传输结果。
167 65
|
1月前
|
Linux
linux中查看某个文件夹下文件的个数和大小
这篇文章介绍了在Linux系统中使用各种命令(如`stat`、`wc`、`du`和`ls`)来查看文件夹下文件的个数和大小的方法。
425 4
linux中查看某个文件夹下文件的个数和大小
|
18天前
|
Linux Shell
Linux系统文件默认权限
Linux系统文件默认权限
34 2
|
2月前
|
Linux
Linux下使用ls查看文件颜色全部为白色的解决方法,以及Linux中文件颜色介绍
Linux下使用ls查看文件颜色全部为白色的解决方法,以及Linux中文件颜色介绍
147 2
|
10天前
|
Linux 开发工具 数据安全/隐私保护
linux异常一:feng 不在 sudoers 文件中,此事将被报告。yum提示Another app is currently holding the yum lock; waiting for
这篇文章介绍了在CentOS 7系统中安装Docker时遇到的两个常见问题及其解决方法:用户不在sudoers文件中导致权限不足,以及yum被锁定的问题。
23 2
linux异常一:feng 不在 sudoers 文件中,此事将被报告。yum提示Another app is currently holding the yum lock; waiting for
|
6天前
|
Linux Shell 数据库
Linux文件查找新姿势:总有一种你没见过
文件查找是Linux用户提升工作效率的关键技能。本文介绍了几种不常见的文件查找方法,包括使用`find`结合`column`美化输出、利用`locate`和`mlocate`快速查找、编写Shell脚本自动化任务、使用现代工具`fd`以及结合`grep`和`rg`进行内容搜索。此外,还推荐了几款图形界面搜索工具。掌握这些技巧,让你的文件查找更加高效便捷。
28 2
|
13天前
|
Linux C++
Linux c/c++文件的基本操作
在Linux环境下使用C/C++进行文件的基本操作,包括文件的创建、写入、读取、关闭以及文件描述符的定位。
13 0
Linux c/c++文件的基本操作
|
18天前
|
移动开发 Linux
Linux 文件与目录管理
Linux 文件与目录管理
17 3
|
18天前
|
关系型数据库 MySQL Linux
Linux 文件基本属性
Linux 文件基本属性
15 3
|
27天前
|
安全 Linux 数据安全/隐私保护
探索Linux操作系统的文件权限管理
【9月更文挑战第29天】在数字世界中,文件权限管理如同保护我们隐私的锁。本文将带你了解如何在Linux系统中设置和管理文件权限,确保你的数据安全。我们将一起学习如何通过命令行工具来控制文件访问,就像学习一门新语言一样有趣。准备好了吗?让我们一起开启这场技术之旅!