天天看點

linux檢視檔案内容行數,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指令來檢視你想了解的指令吧。