linux通過grep根據關鍵字查詢日誌檔案上下文

2021-09-26 12:45:36 字數 4636 閱讀 7420

linux通過grep根據關鍵字查詢日誌檔案上下文

1、在標準unix/linux下的grep命令中,通過以下引數控制上下文的顯示:

grep -c 10 keyword catalina.out 顯示file檔案中匹配keyword字串那行以及上下10行

grep -b 10 keyword catalina.out 顯示keyword及前10行

grep -a 10 keyword catalina.out 顯示keyword及後10行

2、查詢的結果比較大時,為了方便定位問題,也可以重定向到檔案中,比如:

grep -c 10 keyword catalina.out > aaa.txt

3、統計包含某個關鍵字的個數

grep -o keyword catalina.out | wc -l

4、如果一行最多乙個關鍵字,可以簡寫為:

grep -c keyword catalina.out

5、檢視grep版本的方法是

grep -v

grep命令詳細說明:

usage: grep [option]… pattern [file]…

search for pattern in each file or standard input.

pattern is, by default, a basic regular expression (bre).

example: grep -i 『hello world』 menu.h main.c

regexp selection and interpretation:

-e, --extended-regexp pattern is an extended regular expression (ere)

-f, --fixed-strings pattern is a set of newline-separated fixed strings

-g, --basic-regexp pattern is a basic regular expression (bre)

-p, --perl-regexp pattern is a perl regular expression

-e, --regexp=pattern use pattern for matching

-f, --file=file obtain pattern from file

-i, --ignore-case ignore case distinctions

-w, --word-regexp force pattern to match only whole words

-x, --line-regexp force pattern to 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 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 file name for each match

-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 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, --recursive like --directories=recurse

-r, --dereference-recursive

likewise, but follow all symlinks

–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

–group-separator=sep use sep as a group separator

–no-group-separator use empty string as a group separator

–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)

-u, --unix-byte-offsets report offsets as if crs were not there

(msdos/windows)

『egrep』 means 『grep -e』. 『fgrep』 means 『grep -f』.

direct invocation as either 『egrep』 or 『fgrep』 is deprecated.

when file is -, read standard input. with no file, read . if a command-line

-r is given, - otherwise. if fewer than two files are given, assume -h.

exit status is 0 if any line is selected, 1 otherwise;

if any error occurs and -q is not given, the exit status is 2.

report bugs to: [email protected]

gnu grep home page:

general help using gnu software:

linux使用grep命令根據檔案內容搜素檔案

如果你想在當前目錄下 查詢 hello,world 字串,可以這樣 grep rn hello,world 命令格式 grep 選項 pattern 檔案或目錄選項 含義 r 是遞迴查詢 n是顯示行號 r查詢所有檔案包含子目錄 i忽略大小寫 l只列出匹配的檔名,l列出不匹配的檔名,w只匹配整個單詞,...

Linux下grep查詢包含某個關鍵字的上下文資訊

標準unix linux下的grep通過以下引數控制上下文 grep c 5 foo file 顯示file檔案中匹配foo字串那行以及上下5行 grep b 5 foo file 顯示foo及前5行 grep a 5 foo file 顯示foo及後5行 如果想要把這些內容匯入到乙個指定的檔案可以...

grep 基於關鍵字搜尋

grep linux etc passwd 搜尋passwd檔案下的包含linux的行 find user linux grep video 在使用者為linux的根目錄下搜房video內容 netstat tnpl grep server 查詢網路程序為server的資料 i在搜尋的時候忽略大小寫...