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-regexpPATTERN 是一個可擴充套件的正則表示式(縮寫為 ERE) -F, --fixed-stringsPATTERN 是一組由斷行符分隔的定長字串。 -G, --basic-regexpPATTERN 是一個基本正則表示式(縮寫為 BRE) -P, --perl-regexpPATTERN 是一個 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-messagessuppress error messages -v, --invert-matchselect non-matching lines -V, --versionprint version information and exit --helpdisplay this help and exit --mmapignored for backwards compatibility Output control: -m, --max-count=NUMstop after NUM matches -b, --byte-offsetprint the byte offset with output lines -n, --line-numberprint line number with output lines --line-bufferedflush output on every line -H, --with-filenameprint the filename for each match -h, --no-filenamesuppress the prefixing filename on output --label=LABELprint LABEL as filename for standard input -o, --only-matchingshow only the part of a line matching PATTERN -q, --quiet, --silentsuppress all normal output --binary-files=TYPEassume that binary files are TYPE; TYPE is `binary', `text', or `without-match' -a, --textequivalent to --binary-files=text -Iequivalent to --binary-files=without-match -d, --directories=ACTIONhow to handle directories; ACTION is `read', `recurse', or `skip' -D, --devices=ACTIONhow to handle devices, FIFOs and sockets; ACTION is `read' or `skip' -R, -r, --recursiveequivalent to --directories=recurse --include=FILE_PATTERNsearch only files that match FILE_PATTERN --exclude=FILE_PATTERNskip files and directories matching FILE_PATTERN --exclude-from=FILEskip files matching any file pattern from FILE --exclude-dir=PATTERNdirectories that match PATTERN will be skipped. -L, --files-without-matchprint only names of FILEs containing no match -l, --files-with-matchesprint only names of FILEs containing matches -c, --countprint only a count of matching lines per FILE -T, --initial-tabmake tabs line up (if needed) -Z, --nullprint 0 byte after FILE name Context control: -B, --before-context=NUMprint NUM lines of leading context -A, --after-context=NUMprint NUM lines of trailing context -C, --context=NUMprint NUM lines of output context -NUMsame as --context=NUM --color[=WHEN], --colour[=WHEN]use markers to highlight the matching strings; WHEN is `always', `never', or `auto' -U, --binarydo not strip CR characters at EOL (MSDOS) -u, --unix-byte-offsetsreport 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
命令來檢視你想了解的命令吧。