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 ...
随机推荐
- redis高可用 - Master&Slave
Master&Slave也就是我们所说的主从复制,即主机数据更新后根据配置和策略,自动同步到备机的机制.其中Master以写为主,Slave以读为主. Master&Slave的作用主 ...
- ThinkPHP 3.2 调用自定义函数库
ThinkPHP3.2 和3.1 区别还是有点大的 引入了命名空间,强化了驱动化和行为,增强了模块化的概念和对云平台的支持,并改进了诸多的细节.主要改进包括:模块化架构的全新设计全新命名空间和自动导入 ...
- Python 实现图片上表格的写入
直接上代码:import matplotlib.pylab as pltimport numpy as npplt.figure()axes=plt.gca()y= np.random.randn(9 ...
- 移动APP测试要点总结
***微信扫一扫,关注“python测试开发圈”,了解更多测试教程!***
- PowerDesigner16工具学习笔记-建立BPM
根据不同用途,BPM分为分析性(Analysis).执行型(Executable)和协作型(Collaborative) BPM的类型 业务流程语言 描述 分析型 Analysis 提供流程层次 ...
- cf812B 搜索
B. Sagheer, the Hausmeister time limit per test 1 second memory limit per test 256 megabytes input s ...
- APP推广运营经验总结
这片文章来自于我在公司的分享会,主题是关于APP在渠道方面的推广,主要包括3个方面,下载量,留存率,日活跃用户. 首先,在应用市场中,一个APP有四个方面,简介,截图,下载量,评论.用户看这四个方面, ...
- 阅读和设计源码利器UML
https://www.w3cschool.cn/uml_tutorial/ 就不做特殊说明了啊: 一 UML 1 简介 类注释: 下面的图表示的 UML 类,该图被分为四个部分. 顶端部分被用来命名 ...
- 正则,String中用法,Pattern Matcher
package com.正则表达式; import java.util.Scanner; /** * * 校验qq号码 * 1:要求必须是5-15位数字 * 2: 0不能开头 * 分析: * A:键盘 ...
- 面试题总结(一)、TCP协议
声明:本文主要探讨当TCP协议出现在面试笔试场合可能会涉及的问题,每一个知识点讨论力求简洁,便于记忆,但讨论深度有限,如要深入研究可点击参考链接,希望对正在找工作的同学有点帮助. 一.TCP协议简介 ...