Linux常用命令之-grep
简介
grep全称Global Regular Expression Print是一种强大的文本搜索工具,它能使用给定的正则表达式按行搜索文本输出,文件,目录等,统计并输出匹配的信息,grep在文本查找方面非常强悍,也是linux命令中最常用的命令之一
使用grep --help可以查看grep的语法说明,但grep的选项如此之多,以至于在不太熟悉的情况下一下看到太多的选项显得有些懵,本文谨以实用原则结合案例总结出grep的常见用法,最后在给出命令的详细说明,文中所有的项选项笔者都给出完整的英文单词
1.过滤(匹配)输出
查找在运行中的的某个进程大概算的上是非常常用的命令了,使用ps -ef 查看服务器所有运行中的进程,以查找sshd的进程为例
$ ps -ef | grep sshd
输出如下:
root 1290 1 0 19:07 ? 00:00:00 /usr/sbin/sshd
root 1607 1290 0 23:09 ? 00:00:00 sshd: root@pts/0
root 1640 1611 0 23:20 pts/0 00:00:00 grep sshd
但这会将grep sshd这个进程也输出出来,虽然大多数时候 并不影响我们用肉眼去观察,但是有些情况下,我们总是希望把包含grep的这个进程过滤掉,使用-v (invert-match)选项不显示匹配到的行
$ ps -ef | grep sshd | grep -v grep
2.在文件中查找
在文件中查找的基本命令格式为grep string file_name,意为在file_name文件中查找string并输出,加入test.txt内容如下:
abc
def
Abc
想要在文件中查找字符串abc只需使用
$ grep abc test.txt
-------------------
abc
加入有以下场景,test.txt文件很大,我们想要知道查出来的内容在哪一行,以便我们编辑文件的时候能够迅速的定位该位置,这时使用-n(line-number)命令显示行号
$ grep -n abc test.txt
--------------------
1:abc
但上面的命令并没有匹配到Abc这一行,虽然这是我们想要看到的结果,但有些场景下,我们可能有忽略大小写的需求,这里使用-i(ignore-case) 忽略大小写。
$ grep -i abc test.txt
--------------------
abc
Abc
根据正则匹配
考虑以下场景,要从以下的网络日志中显示出所有的来访IP(来访IP为第一个|前的内容)
#access.log
----------------
11.0.21.12|-|-|[10/Aug/2018:17:47:42 +0800]
11.0.23.13|-|-|[10/Aug/2018:17:47:42 +0800]
140.143.145.44|111.111.111|-|[10/Aug/2018:17:47:42 +0800]
使用-E(extended-regexp)命令根据正则匹配
$ grep -E '^([0-9]{1,3}[\.]){3}[0-9]{1,3}' access.log
-------------------------------------
11.0.21.12|-|-|[10/Aug/2018:17:47:42 +0800]
11.0.23.13|-|-|[10/Aug/2018:17:47:42 +0800]
140.143.145.44|111.111.111.111|-|[10/Aug/2018:17:47:42 +0800]
但这会把匹配到的行都匹配出来,使用-o(only-matching)命令只输出匹配到的字符串
$ grep -Eo '^([0-9]{1,3}[\.]){3}[0-9]{1,3}' access.log
----------------------------------
11.0.21.12
11.0.23.13
140.143.145.44
由于正则里^限制开头的原因,服务器的IP已经被过滤掉了。
查找目录
写到这里好像grep的用途还是很小,因为一个文件如果不大的情况下,我们想从中查找部分字符串也并不复杂。但当从一个项目拥有很多文件的工程目录中查找内容时,grep就显得很方便了。
考虑项目目录如下
$ ls
apps etl etl_dba.py nohup.out simple.celery.log simple.celery.pid utils
我们要查看所有关于rabbitmq的配置,使用-r(recursive)匹配目录下所有的非二进制文件
$ grep -r amqp ./
./etl/celeryconfig.py:# BROKER_URL = 'amqp://unicorn_etl:unicorn_etl@111.111.1.111:5672/unicorn_etl_hp'
./etl/celeryconfig.py:BROKER_URL = 'amqp://unicorn_etl:unicorn_etl@10.14.50.17:5672/unicorn_etl_hp'
./simple.celery.log: Connected to amqp://unicorn_etl:**@10.14.50.17:5672/unicorn_etl_hp
./simple.celery.log: Connected to amqp://unicorn_etl:**@10.14.50.17:5672/unicorn_etl_hp
使用 -h(no-filename)取消文件名
$ grep -rh amqp ./
# BROKER_URL = 'amqp://unicorn_etl:unicorn_etl@111.111.1.111:5672/unicorn_etl_hp'
BROKER_URL = 'amqp://unicorn_etl:unicorn_etl@10.14.50.17:5672/unicorn_etl_hp'
Connected to amqp://unicorn_etl:**@10.14.50.17:5672/unicorn_etl_hp
Connected to amqp://unicorn_etl:**@10.14.50.17:5672/unicorn_etl_hp
假如现在我们又想修改配置,如果能直接输出文件和行号的话,我们会更轻松的定位文件位置,结合-n命令,可以轻松的做到这一点
$ grep -rn amqp ./
Binary file ./etl/celeryconfig.pyc matches
./etl/celeryconfig.py:14:# BROKER_URL = 'amqp://unicorn_etl:unicorn_etl@111.111.1.111:5672/unicorn_etl_hp'
./etl/celeryconfig.py:15:BROKER_URL = 'amqp://unicorn_etl:unicorn_etl@10.14.50.17:5672/unicorn_etl_hp'
./simple.celery.log:1: Connected to amqp://unicorn_etl:**@10.14.50.17:5672/unicorn_etl_hp
./simple.celery.log:12: Connected to amqp://unicorn_etl:**@10.14.50.17:5672/unicorn_etl_hp
但其实我们可以看到,中间部分数据属于simple.celery.log日志文件的,使用--exclude 排除文件
$ grep -rh --exclude *.log amqp ./
Binary file ./etl/celeryconfig.pyc matches
# BROKER_URL = 'amqp://unicorn_etl:unicorn_etl@111.111.1.111:5672/unicorn_etl_hp'
BROKER_URL = 'amqp://unicorn_etl:unicorn_etl@10.14.50.17:5672/unicorn_etl_hp'
使用--color将匹配到的内容高亮显示
grep -rh --exclude *.log amqp ./ --color
Binary file ./etl/celeryconfig.pyc matches
# BROKER_URL = 'amqp://unicorn_etl:unicorn_etl@115.182.1.152:5672/unicorn_etl_hp'
BROKER_URL = 'amqp://unicorn_etl:unicorn_etl@10.14.50.17:5672/unicorn_etl_hp'
总结
以上几点用法差不多可以搞定大多数日常对grep的需求,但grep提供的功能还远不止这些,使用grep --help查看所有的选项,相信有了上面的基础,根据help输出的提示,结合自己的需求灵活使用grep命令并不是一件困难的事情。
$ grep --help
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)
Linux常用命令之-grep的更多相关文章
- linux常用命令:grep 命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- 【操作系统之五】Linux常用命令之grep
一.概念grep(Global search Regular Expression and Print out the line)强大的文本搜索工具,从文本文件或管道数据流中筛选匹配的行及数据,并把匹 ...
- Linux常用命令之grep
标题:grep命令的使用 作用:grep可以解析一行文字,取得关键字,若该行存在关键字,就会整行列出. grep [-acinv] [--color=auto] '查找字符串' filename ...
- Linux 常用命令七 grep
一.grep命令 grep(global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜 ...
- Linux常用命令touch/grep/mkdir/rm/cat/find/cp/mv/tar/gzip等
Unix-->Linux(Ubuntu,Redhat,suse,fedora) 1. cd - :回到上次执行的那个目录(相当于“回看”的功能) 2. touch :创建一个文件,可以是任意后缀 ...
- linux常用命令的介绍
本文主要介绍Linux常用命令工具,比如用户创建,删除,文件管理,常见的网络命令等 如何创建账号: 1. 创建用户 useradd -m username -m 表示会在/home 路径下添加创建用户 ...
- linux——常用命令与脚本
linux常用命令 --文件管理pwd --查看当前目录cd --切换当前目录ls --列出当前目录下的所有文件touch --创建文件mkdir --建立目录rmdir --删除空目录rm --删除 ...
- DOS 和 Linux 常用命令的对比
DOS 和 Linux 常用命令的对比 许多在 shell 提示下键入的 Linux命令都与你在 DOS 下键入的命令相似.事实上,某些命令完全相同. 本附录提供了 Windows的 DOS 提示下的 ...
- 第一章,Linux常用命令
20161124 Linux常用命令1.find find /etc/ -size +50k -lsfind /etc/ -size +50k -ls 2> /dev/null查看目录下大于50 ...
随机推荐
- spark streaming 使用geoIP解析IP
1.首先将GEOIP放到服务器上,如,/opt/db/geo/GeoLite2-City.mmdb 2.新建scala sbt工程,测试是否可以顺利解析 import java.io.Fileimpo ...
- [java]String和Date、Timestamp之间的转换
一.String与Date(java.util.Date)互转 1.1 String -> Date Date date = DateFormat.parse(String str); St ...
- Android------个人项目(歆语气象通)
歆语气象通: 歆语气象伴随你的身边,便捷生活. 包含了以下功能: 1. 天气预报数据覆盖中国城市和地区:2. 提供一周天气预报及最低最高温度,时刻关注天气,轻松计划出行:3. 各种指数详细信息,如太阳 ...
- 本地RUN Page时报无法显示该网页
经检查,是我本地安装了浏览器广告屏蔽插件引起的,关闭该插件即可.
- Git观察和比较
log git log 时间是从下到上,从远到近 whatchanged git whatchanged 时间是从下到上,从远到近 diff --staged 比较工作区和缓存区之间的差异 g ...
- cdq分治的小结
cdq分治 是一种特殊的分治 他的思想: 1.分治l,mid 2.分治mid+1,r 3.计算l,mid对mid+1,r的影响 3就是最关键的地方 这也是cdq的关键点 想到了这一步基本就可以做了 接 ...
- 《Java程序设计》十四次作业
<Java程序设计>十四次作业实验总结 1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结与数据库相关内容. 3. 代码量统计 周次 总代码量 新增代码量 总文件数 新增 ...
- js:关闭当前页面
var userAgent = navigator.userAgent; if (userAgent.indexOf("Firefox") != -1 || userAgent.i ...
- linux网络监控_网速测试
Linux下查看网络即时网速 1.sar命令(一般般) sar -n DEV 1 100 1代表一秒统计并显示一次 100代表统计一百次 sar在sysstat包 2.使用ntop图形工具(没详细用过 ...
- Linux 定制X86平台操作系统
/********************************************************************************* * Linux 定制X86平台操作 ...