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
什么是撷取命令啊?说穿了,就是将一段数据经过分析后,取出我们所想要的.或者是经由分析关键词,取得我们所想要的那一行! 不过,要注意的是,一般来说,撷取信息通常是针对『一行一行』来分析的, 并不是整篇信 ...
随机推荐
- JNI NDK (AndroidStudio+CMake )实现C C++调用Java代码流程
JNI/NDK Java调用C/C++前言 通过第三篇文章讲解在实际的开发过程中Java层调用C/C++层的处理流程.其实我们在很大的业务里也需要C/C+ +层去调用Java层,这两层之间的相互调用 ...
- Android Studio手动打包
项目写完了,现在需要把应用上传到市场上面,那么怎么把项目打包成apk?(Android的可安装文件). 1. 创建签名文件 2. 填写好签名参数 3. 生成APK 注意:签名的密码和密匙的密码注意保管 ...
- 第二章 运算方法与运算器(浮点数的加减法,IEEE754标准32/64浮点规格化数)
这一章,主要介绍了好多种计算方法.下面,写一点自己对于有些计算(手写计算过程)的见解. 1.原码.反码.补码 原码:相信大家都会写,符号位在前二进制数值在后,凑够位数即可. 反码:原码符号位不变,其他 ...
- 方法调用 Controller的Action 参数
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- [20181116]18c DML 日志优化.txt
[20181116]18c DML 日志优化.txt 1.环境:xxxxxxxx> select banner_full from v$version;BANNER_FULL---------- ...
- VS调式时出现异常,在输入法是中文状态下,输入框输入字母再回车,会造成页面关闭,vs退出调式
解决方案:关闭浏览器窗口关闭时,停止调试 .就恢复正常,无异常
- reportComplaints.js: Uncaught TypeError: Cannot read property 'message' of undefined
vonic 中遇到这么个问题, 一直提示我未定义, 可是明明有定义 var tab={ message:{ number:'', title:'' } } var id= { template: '# ...
- python while and for
一.while循环 1.格式: while 条件: while循环体 else: 循环正常跳出执行的语句 2.实例: index= : : break #直接跳出while ,不会执行else els ...
- jQ not()选择器 与 css3 :not( selector )选择器
1.jQ not() 2.css3 not w3c在线演示地址 http://www.w3school.com.cn/tiy/t.asp?f=css_sel_not 总结: 注意两者还是有区别的 ...
- C#深度学习のTask(基于任务的异步模型)
一.Task关键字解释 Task 类的表示的单个操作不会返回一个值,通常以异步方式执行. Task 对象是一种的中心思想 基于任务的异步编程模式 首次引入.NET Framework 4 中. 因为由 ...