Linux 中如何查看文件的行数,字数,字节数

在 Linux 系统使用中,我们经常需要查看或统计文本文件中的行数,字数,字节数等内容,那么怎么快捷的统计出文件中这些关键数据呢。

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

首先我们介绍下 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的用法,我们来举例说明下:

1、获取文件中行数

wc -l app.log
输出
455452 app.log

2、获取文件中单词数

wc -w app.log
输出
4855263 app.log

3、获取文件中字节

wc -c app.log
输出
95169019 app.log

是不是很简单呢。

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

首先我们看看grep这个命令:

grep --help
用法: grep [选项]... PATTERN [FILE]...
在每个 FILE 或是标准输入中查找 PATTERN。
默认的 PATTERN 是一个基本正则表达式(缩写为 BRE)。
例如: grep -i 'hello world' menu.h main.c

正则表达式选择与解释:
  -E, --extended-regexp     PATTERN 是一个可扩展的正则表达式(缩写为 ERE)
  -F, --fixed-strings       PATTERN 是一组由断行符分隔的定长字符串。
  -G, --basic-regexp        PATTERN 是一个基本正则表达式(缩写为 BRE)
  -P, --perl-regexp         PATTERN 是一个 Perl 正则表达式
  -e, --regexp=PATTERN      用 PATTERN 来进行匹配操作
  -f, --file=FILE           从 FILE 中取得 PATTERN
  -i, --ignore-case         忽略大小写
  -w, --word-regexp         强制 PATTERN 仅完全匹配字词
  -x, --line-regexp         强制 PATTERN 仅完全匹配一行
  -z, --null-data           一个 0 字节的数据行,但不是空行

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             print version information and exit
      --help                display this help and exit
      --mmap                ignored for backwards compatibility

Output control:
  -m, --max-count=NUM       stop after NUM matches
  -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 the filename for each match
  -h, --no-filename         suppress the prefixing filename on output
      --label=LABEL         print LABEL as filename for standard input
  -o, --only-matching       show only the part of a line matching PATTERN
  -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, -r, --recursive       equivalent to --directories=recurse
      --include=FILE_PATTERN  search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN  directories that match PATTERN will be skipped.
  -L, --files-without-match  print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching 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)
  -u, --unix-byte-offsets   report offsets as if CRs were not there (MSDOS)

‘egrep’即‘grep -E’。‘fgrep’即‘grep -F’。
直接使用‘egrep’或是‘fgrep’均已不可行了。
不带 FILE 参数,或是 FILE 为 -,将读取标准输入。如果少于两个 FILE 参数
就要默认使用 -h 参数。如果选中任意一行,那退出状态为 0,否则为 1;
如果有错误产生,且未指定 -q 参数,那退出状态为 2。

我们通过如下命令方式来实现查询匹配到关键字'error'中文件行数。

grep 'error' app.log |wc -l

是不是很简单,如果希望了解更多,不妨通过man命令来查看你想了解的命令吧。

你可能感兴趣的:(Linux 中如何查看文件的行数,字数,字节数)