1. 查询文件中的关键字并高亮显示【查询当前目录关键字为elasticsearch的日志文件】
find ./ -name "my-elasticsearch.log" | xargs grep --color=auto "elasticsearch"
2. 查看日志文件后100行
tail -100f log_file.log
3. 另外一种查询关键字log的方法
grep -rn "二狗子" /logs/web.log --color
4. 查询并打印;匹配行之前和之后各2行
grep -rn '' web.log --color -C
5. 搜索压缩包里面的日志文件
zcat /logs/log.--.log.gz | grep '' --color
或者
zgrep --color '' /logs/log.--.log.gz
满足多个条件查询
# 同时满足多条件
zcat /logs/log.--.log.gz | grep 'A' | grep 'B' --color
zgrep 'A' /logs/log.--.log.gz | grep 'B' --color
满足任一条件
# 满足任一条件
zcat /logs/log.--.log.gz | grep -E 'A|B|C' --color
zgrep --color -E 'A|B|C' /logs/log.--.log.gz
6. 对接外部路由,用telnet查看地址通不通
# 目标地址是ip
$ telnet -b [本机地址] [目标地址] # 目标地址是url,不要带http://
$ telnet -b [本机地址] xxx.api.com
Trying xxx.xx.xx.xxx...
Connected to xxx.api.com.
Escape character is '^]'.
7. 统计特定字符出现次数
grep 'key' app.log | wc -l
8. grep和zgrep定义
================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 byte, not newline 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 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' means `grep -E'. `fgrep' means `grep -F'.
Direct invocation as either `egrep' or `fgrep' is deprecated.
With no FILE, or when FILE is -, read standard input. If less than two FILEs
are given, assume -h. Exit status is if any line was selected, otherwise;
if any error occurs and -q was not given, the exit status is .
================zgrep================
Usage: /usr/bin/zgrep [OPTION]... [-e] PATTERN [FILE]...
Look for instances of PATTERN in the input FILEs, using their
uncompressed contents if they are compressed. OPTIONs are the same as for 'grep'.