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 APP 帧数FPS的方法
(下述需要先安装eclipse,不然无法抓包) 1.保证手机与PC连接是正常的 2.打开手机“设置”→“开发者选项”(没有开发者选项就点击“关于手机”“版本号”连续点击就会出现开发者选项了).找到监控 ...
- 适配器Adapter
#!/usr/bin/env python # -*- coding: utf-8 -*- # @Time : 2019/3/4 22:13 # @Author : ChenAdong # @emai ...
- 剑指offer(java版)【转】
面试题 2 :实现单例模式 1. 饿汉式单例类 public class SingletonClass { private static final SingletonClass instance=n ...
- kali linux源大全
输入leafpad /etc/apt/sources.list进入 #官方源 deb http://http.kali.org/kali kali main non-free contr ...
- C语音输出前100个回文素数,每行10个,适当对齐
#include<stdio.h> #include<math.h> int ss(long n) { ); ) ; ;i<=sqrt(n);i++) ); ; } lo ...
- NodeJS二进制包安装和快捷键配置(适用于U盘版安装配置)
首先下载NodeJS二进制安装包:https://nodejs.org/dist/v10.15.3/node-v10.15.3-win-x64.zip 在D盘新建NodeJS文件夹,解压node-v1 ...
- php的register_long_arrays可以关闭以提高性能
如果你使用php5.0以上,在程序中没有使用$HTTP_*_VARS这种变量,那么就可以考虑将register_long_arrays设置为Off,来提高一点点性能.看下PHP手册中关于registe ...
- (转)Spring Boot(十七):使用 Spring Boot 上传文件
http://www.ityouknow.com/springboot/2018/01/12/spring-boot-upload-file.html 上传文件是互联网中常常应用的场景之一,最典型的情 ...
- 设计模式のMediatorPattern(中介者模式)----行为模式
一.产生背景 从生活中的例子可以看出,不论是QQ游戏还是QQ群,它们都是充当一个中间平台,QQ用户可以登录这个中间平台与其他QQ用户进行交流,如果没有这些中间平台,我们如果想与朋友进行聊天的话,可能就 ...
- @RequestParam 和@RequestBody 的区别?
@RequestParam用来接收: 1 用来处理简单的参数绑定 2 用来接收 Content-Type 是 application/x-www-form-urlencoded (这种格 式的数据 ...