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
什么是撷取命令啊?说穿了,就是将一段数据经过分析后,取出我们所想要的.或者是经由分析关键词,取得我们所想要的那一行! 不过,要注意的是,一般来说,撷取信息通常是针对『一行一行』来分析的, 并不是整篇信 ...
随机推荐
- turnserver 配置说明记录
coTurn工程提供了较完整的STUN和TURN服务,记录其主要的命令行参数配置说明 针对TURN/STUN服务进程turnserver.exe的使用参数做简单说明 -L 监听的IP地址 -p 监听端 ...
- RAS非对称加密与数字证书数字签名
它用图片通俗易懂地解释了,"数字签名"(digital signature)和"数字证书"(digital certificate)到底是什么. 我对这些问题的 ...
- JAVA多线程的问题以及处理(二)【转】
使用互斥解决多线程问题是一种简单有效的解决办法,但是由于该方法比较简单,所以只能解决一些基本的问题,对于复杂的问题就无法解决了. 解 决多线程问题的另外一种思路是同步.同步是另外一种解决问题的思路,结 ...
- C#面向对象 1
using System; using System.Collections.Generic; using System.Collections; using System.Linq; using S ...
- 使用 boost.asio 简单实现 异步Socket 通信
客户端: class IPCClient { public: IPCClient(); ~IPCClient(); bool run(); private: bool connect(); bool ...
- PHP下载网页
<?php /* author:whq 作用:获取网页的内容 */ include "../Snoopy/Snoopy.class.php";class Cute ...
- C++借助curses库实现俄罗斯方块
主要要实现如下几个功能:方块的移动控制.方块变形.判定方块是否接触边界和进行方块堆积.对方块进行消除. 1.方块的移动控制上下左右四个方向上-->变形,下-->加速下落,左-->向左 ...
- Fetch请求后台的数据
<style> #btn{ width: 50px; height: 50px; background-color: red; } #output{ width: 100px; heigh ...
- ACE侧边栏刷新自动展开之前的选择
在body下面加上 <script type="text/javascript"> $(document).ready(function(){ var url = do ...
- NGINX Load Balancing – TCP and UDP Load Balancer
This chapter describes how to use NGINX Plus and open source NGINX to proxy and load balance TCP and ...