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
什么是撷取命令啊?说穿了,就是将一段数据经过分析后,取出我们所想要的.或者是经由分析关键词,取得我们所想要的那一行! 不过,要注意的是,一般来说,撷取信息通常是针对『一行一行』来分析的, 并不是整篇信 ...
随机推荐
- Android 源码编译之旅
目录 前言 背景 安装软件 正文 Mac 分区 移动硬盘分区 Repo 下载源码 编译 源码导入 Android Studio 查看 碰到的问题 Could not find a supported ...
- onmouseover和onmouseenter区别
onmouseover和onmouseenter都是鼠标进入时触发,onmouseover在所选元素的子元素间切换的时候也触发! <!doctype html><html lang= ...
- RMAN命令DELETE操作总结
本篇总结一下RMAN命令中的DELETE操作,DELETE命令用于删除RMAN备份记录以及相应的物理文件. To delete physical backups and copies as well ...
- ADV190007 - “PrivExchange” 特权提升漏洞的指南
Microsoft Exchange Server中存在一个特权提升漏洞.成功利用此漏洞的攻击者可能会尝试模仿Exchange服务器的任何其他用户.要利用此漏洞,攻击者需要执行中间人攻击才能将身份验证 ...
- Java监听器Listener使用详解
监听器用于监听web应用中某些对象.信息的创建.销毁.增加,修改,删除等动作的发生,然后作出相应的响应处理.当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法.常用于统计在线人数和在线用 ...
- JavaScript的基本包装类型概述与基本包装类型_Number类型
JavaScript的基本包装类型示例 为了便于操作基本类型值,javaScript 提供了 3 个特殊的引用类型:Boolean.Number和 String. 这些类型与其他引用类型相似,但同时也 ...
- Scrapy 框架 增量式
增量式: 用来检测网站中数据的更新情况 from scrapy.linkextractors import LinkExtractor from scrapy.spiders import Crawl ...
- Get https://192.168.2.119/v2/: dial tcp 192.168.2.119:443: getsockopt: connection refused
Get https://192.168.2.119/v2/: dial tcp 192.168.2.119:443: getsockopt: connection refused
- ddt框架优化(生成html报告注释内容传变量)
https://blog.csdn.net/weixin_33923148/article/details/86017742 按要求修改后发现 注释只传值第一个变量 这是因为 ddt数据驱动生成ht ...
- 【转】curl命令总结,Http Post_Get 常用
curl命令总结 curl 是一个利用URL语法在命令行方式下工作的文件传输工具.它支持很多协议:FTP, FTPS, HTTP, HTTPS, GOPHER, TELNET, DICT, FILE ...