Linux每天一个命令:grep
grep (缩写来自Globally search a Regular Expression and Print)
是一种强大的文本搜索工具,它能使用特定模式匹配(包括正则表达式)搜索文本,并默认输出匹配行。Unix的grep家族包括grep、egrep和fgrep。Windows系统下类似命令FINDSTR。
基本简介
表达符集
匹配文本
[root@localhost ~]# cat grep.txt
linuxhood
linxuhood shell
linux hoodpython
python is good
shell is good
[root@localhost ~]# grep ^python grep.txt
python is good
[root@localhost ~]# grep good$ grep.txt
python is good
shell is good
[root@localhost ~]# grep go.d grep.txt
python is good
shell is good
POSIX字符类
命令选项:
实例:
实例1:查找指定进程
[root@localhost ~]# ps -ef| grep 'sshd' | grep -v 'grep' #-v去掉grep本身执行进程
root Oct31 ? :: /usr/sbin/sshd -D
root : ? :: sshd: root@pts/
root Nov29 ? :: sshd: root@pts/
root Nov30 ? :: sshd: root@pts/
实例2:查找指定进程个数
[root@localhost ~]# ps -ef| grep 'sshd' | grep -v 'grep' -c
实例3:从文件中读取关键词进行搜索
[root@localhost ~]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost ~]# cat rege.txt
l.*x
Redhat
[root@localhost ~]# cat test.txt | grep -f rege.txt
hnlinux
ubuntu linux
Redhat
linuxmint
说明:
输出test.txt文件中含有从rege.txt文件中读取出的关键词的内容行
实例3:从文件中查找关键词
[root@localhost ~]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost ~]# grep 'l.*x' test.txt
hnlinux
ubuntu linux
linuxmint
[root@localhost ~]# grep -n 'l.*x' test.txt
:hnlinux
:ubuntu linux
:linuxmint
实例4:从多个文件查找关键词
[root@localhost ~]# cat test.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost ~]# cat test1.txt
hnlinux
peida.cnblogs.com
ubuntu
ubuntu linux
redhat
Redhat
linuxmint
[root@localhost ~]# grep -n 'l.*x' test.txt test1.txt
test.txt::hnlinux
test.txt::ubuntu linux
test.txt::linuxmint
test1.txt::hnlinux
test1.txt::ubuntu linux
test1.txt::linuxmint
实例5:显示当前目录下以.txt 结尾的文件中的所有包含每个字符串至少有7个连续小写字符的字符串的行
[root@localhost ~]# grep -nE '[a-z]{7,}' *.txt
grep.txt::linuxhood
grep.txt::linxuhood shell
grep.txt::linux hoodpython
gtest.txt::loversrs
gtest.txt::mariadb
gtest.txt::loverss
no_rege.txt::loverss
test1.txt::hnlinux
test1.txt::peida.cnblogs.com
test1.txt::linuxmint
test.txt::hnlinux
test.txt::peida.cnblogs.com
test.txt::linuxmint
Linux每天一个命令:grep的更多相关文章
- LINUX上一个命令计算PI
		Linux上一个命令计算PI – 笑遍世界 http://smilejay.com/2017/11/calculate-pi-with-linux-command/ [root@d1 goEcho]# ... 
- Linux日常之命令grep
		命令grep简介 利用该命令在文本中查找指定的字符串,是Linux中最常用的文本处理工具之一. 命令grep与正则表达式结合使用时,功能会非常强大. 命令grep会在文本文件中按照指定的正则表达式进行 ... 
- Linux查找字符串命令grep(转)
		Linux grep命令用于查找文件里符合条件的字符串. grep指令用于查找内容包含指定的范本样式的文件,如果发现某文件的内容符合所指定的范本样式,预设grep指令会把含有范本样式的那一列显示出来. ... 
- Linux文本处理命令 -- grep
		简介 grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它 ... 
- Linux每天一个命令:iperf
		iperf命令 Iperf 是一个网络性能测试工具.Iperf可以测试最大TCP和UDP带宽性能,具有多种参数和UDP特性,可以根据需要调整,可以报告带宽.延迟抖动和数据包丢失.下载地址:https: ... 
- Linux每天一个命令:tar
		Linux tar命令简介: tar命令可以为linux的文件和目录创建档案.利用tar,可以为某一特定文件创建档案(备份文件),也可以在档案中改变文件,或者向档案中加入新的文件.tar最初被用来在磁 ... 
- Linux每天一个命令:cat
		Linux cat命令 命令:cat cat 命令用于连接文件并打印到标准输出设备上. 使用权限 所有使用者 语法格式 cat [-AbeEnstTuv] [--help] [--version] f ... 
- Linux每天一个命令:nc/ncat
		nmap-ncat.x86_64版nc/ncat nc/ncat所做的就是在两台电脑之间建立链接并返回两个数据流,在这之后所能做的事就看你的想像力了.你能建立一个服务器,传输文件,与朋友聊天,传输流媒 ... 
- 【Linux】撷取命令grep
		什么是撷取命令啊?说穿了,就是将一段数据经过分析后,取出我们所想要的.或者是经由分析关键词,取得我们所想要的那一行! 不过,要注意的是,一般来说,撷取信息通常是针对『一行一行』来分析的, 并不是整篇信 ... 
随机推荐
- C# 动态加载程序集信息
			本文通过一个简单的实例,来讲解动态加载Dll需要的知识点.仅供学习分享使用,如有不足之处,还请指正. 在设计模式的策略模式中,需要动态加载程序集信息. 涉及知识点: AssemblyName类,完整描 ... 
- Android为TV端助力 post带数据请求方式,传递的数据格式包括json和map
			如下: public static String httpPost(String url, String json) { try { URL u = new URL(url); HttpURLConn ... 
- Play 2D games on Nexus 6P running Android N7.1.1 with Daydream View VR headset
			http://files.cnblogs.com/files/we-hjb/N6P_Android7_SBS_SF.rar 
- iOS开发GCD(3)-数据安全
			/* 多个线程可能访问同一块资源,造成数据错乱和数据安全问题 为代码添加同步锁(互斥锁) */ -(void)synchronized{ @synchronized(self){ //需要锁住的代码, ... 
- java I/O工作机制
			java I/O 的基本架构: 1:基于字节操作的I/O接口 InputStream OutputStream 2:基于字符操作的I/O接口 Writer 和Reader 3:基于磁盘操作的I/O接口 ... 
- day11(python)装饰器
			def wrapper(f):#1 def inner(*args,**kwargs):#3 ret = f(*args,**kwargs)#5 return ret#8 return inner#4 ... 
- Spark操作HBase报:org.apache.hadoop.hbase.client.RetriesExhaustedWithDetailsException异常解决方案
			一.异常信息 19/03/21 15:01:52 WARN scheduler.TaskSetManager: Lost task 4.0 in stage 21.0 (TID 14640, hnte ... 
- 使用IEDriverServer.exe驱动IE11,实现自动化测试
			+ 下载IEDriverServer http://dl.pconline.com.cn/download/771640-1.html 解压缩得到IEDriverServer.e ... 
- postgresql自定义类型并返回数组
			转自 https://blog.csdn.net/victor_ww/article/details/44415895 create type custom_data_type as ( id int ... 
- 使用epoll实现聊天室功能,同时比较epoll和select的异同
			1.首先介绍一下select和epoll的异同,如下(摘抄自https://www.cnblogs.com/Anker/p/3265058.html) select的几大缺点: (1)每次调用sele ... 
