grep命令

作用:从文本文件或管道数据流中筛选匹配的行及数据,配合正则表达式一起使用,功能更加强大。

格式:

grep [options] [pattern] [file]

1,匹配包含"ghostwu"的行

ghostwu@dev:~/linux/grep$ cat -n ghostwu.txt
my name is ghostwu
how are you
fine think you
My name is Ghostwu
what's your name?
my name is ghostwu2 ghostwu@dev:~/linux/grep$ grep "ghostwu" ghostwu.txt
my name is ghostwu
my name is ghostwu2

2,-v: 不包含,相当于取反

ghostwu@dev:~/linux/grep$ grep -v "ghostwu" ghostwu.txt
how are you
fine think you
My name is Ghostwu
what's your name? ghostwu@dev:~/linux/grep$

3,-n 添加行号

ghostwu@dev:~/linux/grep$ grep -n "ghostwu" ghostwu.txt
:my name is ghostwu
:my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -vn "ghostwu" ghostwu.txt
:how are you
:fine think you
:My name is Ghostwu
:what's your name?
:

4,-E,使用扩展的egrep命令,模式中可以用正则表达式

ghostwu@dev:~/linux/grep$ cat ghostwu.txt
my name is ghostwu
how are you
fine think you
My name is Ghostwu
what's your name?
my name is ghostwu2 ghostwu@dev:~/linux/grep$ grep -E "my|your" ghostwu.txt
my name is ghostwu
what's your name?
my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -Ev "my|your" ghostwu.txt
how are you
fine think you
My name is Ghostwu ghostwu@dev:~/linux/grep$ grep -En "my|your" ghostwu.txt
:my name is ghostwu
:what's your name?
:my name is ghostwu2

5,-i选项,不区分大小写

ghostwu@dev:~/linux/grep$ grep "ghostwu" ghostwu.txt
my name is ghostwu
my name is ghostwu2
ghostwu@dev:~/linux/grep$ grep -i "ghostwu" ghostwu.txt
my name is ghostwu
My name is Ghostwu
my name is ghostwu2

6,-c :统计匹配的行数,不是匹配字符串的次数

ghostwu@dev:~/linux/grep$ grep -c "ghostwu" ghostwu.txt 

ghostwu@dev:~/linux/grep$ grep -ci "ghostwu" ghostwu.txt
ghostwu@dev:~/linux/grep$ grep -c "ghostwu" ghostwu.txt 

ghostwu@dev:~/linux/grep$ grep "ghostwu" ghostwu.txt
my name is ghostwu, nice to meet you,ghostwu
my name is ghostwu2
ghostwu@dev:~/linux/grep$ cat -n ghostwu.txt
my name is ghostwu, nice to meet you,ghostwu
how are you
fine think you
My name is Ghostwu
what's your name?
my name is ghostwu2

7,-o: 只输出匹配到的字符串

ghostwu@dev:~/linux/grep$ grep -o "ghostwu" ghostwu.txt
ghostwu
ghostwu
ghostwu@dev:~/linux/grep$ grep -oi "ghostwu" ghostwu.txt
ghostwu
Ghostwu
ghostwu

8,-w: 只匹配过滤的单词,类似于精确匹配

ghostwu@dev:~/linux/grep$ grep -w "ghostwu" ghostwu.txt
my name is ghostwu, nice to meet you,ghostwu
ghostwu@dev:~/linux/grep$ grep -wi "ghostwu" ghostwu.txt
my name is ghostwu, nice to meet you,ghostwu
My name is Ghostwu
ghostwu@dev:~/linux/grep$ cat -n ghostwu.txt
my name is ghostwu, nice to meet you,ghostwu
how are you
fine think you
My name is Ghostwu
what's your name?
my name is ghostwu2

9,常用的一招小技巧,去除文件的注释和空行,在运维中,可以用这条命令把配置文件的空行和注释去掉,然后用管道生成。这样配置文件比较容易查看和配置

ghostwu@dev:~/linux/grep$ grep -Ev "^$|#" ghostwu.php
<?php
class Person {
public $name = 'ghostwu';
public $age = ;
public function showinfo(){
echo $this->name . PHP_EOL;
echo $this->age. PHP_EOL;
}
}
ghostwu@dev:~/linux/grep$ cat -n ghostwu.php
<?php class Person { #人名
public $name = 'ghostwu'; #年龄
public $age = ; #显示信息 
public function showinfo(){
echo $this->name . PHP_EOL;
echo $this->age. PHP_EOL;
}
}

Linux常用基本命令:grep-从文件或者管道中筛选匹配的行的更多相关文章

  1. Linux常用基本命令wc-统计文件的字节,字符,行数

    wc命令 作用:统计文件的字节,单词,行数 用法:wc [option] [file] -c:统计字节 ghostwu@dev:~/linux/uniq$ cat ghostwu.txt 192.16 ...

  2. linux常用基本命令 grep awk 待优化

    查看centos操作系统版本:cat /etc/centos-release 切换到当前用户主目录:cd 或者cd ~ 创建文件夹/a/b/c:mkdir -pv /a/b/c.如果/a/b/c的父目 ...

  3. Linux 常用基本命令及应用技巧

    需要pdf 版 联系我 我的文件中有目录一.Linux 的常用基本命令................................................................. ...

  4. Linux常用基本命令(less)

    转: Linux常用基本命令(less) LESS:跟more命令的功能类似,都是用于分页显示内容,但是他的性能比more更高,功能比more更丰富,他读取文件是按需加载 格式: less [opti ...

  5. linux常用基本命令

    Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们.   系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器 ...

  6. Linux常用命令01(文件和目录)

    目标 查看目录内容 ls 切换目录 cd 创建和删除文件 touch rm mkdir 拷贝和移动文件 cp mv 查看文件内容 cat more grep 其他 echo 重定向 > 和 &g ...

  7. linux常用基本命令整理小结

    linux系统遵循的基本原则 由目标单一的小程序组成,组合小程序完成复杂任务: 一切皆文件: 尽量避免捕捉用户接口: 配置文件保存为纯文本文件: Linux命令行常识 命令格式 命令+选项+参数 选项 ...

  8. Linux常用命令(第二版) --文件搜索命令

    文件搜索命令 1.which /usr/bin/which #显示系统命令所在目录,绝对目录,不能查找文件 格式:which [系统命令] e.g. which ls 附-whereis:也可以查找到 ...

  9. linux常用命令 grep命令

    linux grep命令 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配行打印出来 grep 全称 Grobal Regular Expression Pr ...

随机推荐

  1. hdu 4027 Can you answer these queries?[线段树]

    题目 题意: 输入一个 :n  .(1<=n<<100000) 输入n个数    (num<2^63) 输入一个m :代表m个操作    (1<=m<<100 ...

  2. pyinstaller打包程序 带图片

    首选说一下,这种打包方式只能在本电脑上使用运行正常 准备:.py文件:你的程序 gif文件:你要用的图片 第一步: 在上面文件所在目录打开cmd 输入 pyi-makespec FP.py 会生成一个 ...

  3. 三菱蓝瑟CK4A

    日本JDM蓝瑟,而且还是MR的性能版,避震行程也是这么长的.证明这个车子就是这样设计的. 90年代拉力血统的车就这样? 东南厂国产的蓝瑟,原装避震是厦门开发生产,来自于台湾开发工业集团的全资子公司,而 ...

  4. Android开发的插件Code Generator与LayoutCreator的安装与使用,提升你的开发效率

    前言 大家好,给大家带来Android开发的插件Code Generator与LayoutCreator的安装与使用,提升你的开发效率的概述,希望你们喜欢 学习目标 掌握两个插件的安装和使用,能够实现 ...

  5. 【微服务】.netCore eShopOnContainers 部署实践《二》

    Docker 专业术语介绍 优点:轻量级.可伸缩(灵活性).可靠性.可移植  Container image A package with all of the dependencies and in ...

  6. Java开发技术大揭底——让你认知自己技术上的缺陷,成为架构师

    一.分布式架构体系 分布式怎么来的.传统的电信.银行业,当业务量大了之后,普通服务器CPU/IO/网络到了100%,请求太慢怎么办?最直接的做法,升级硬件,反正也不缺钱,IBM小型机,大型机,采购了堆 ...

  7. python之内置装饰器(property/staticmethod/classmethod)

    python内置了property.staticmethod.classmethod三个装饰器,有时候我们也会用到,这里简单说明下 1.property 作用:顾名思义把函数装饰成属性 一般我们调用类 ...

  8. Linux内核源码分析之调度、内核线程模型 And Centos7.2's Kernel Resource Analysis

    本文分为概述.历史.el7.2代码架构图解三部分. 解决的问题: a.Kernel调度发展过程: b.以架构图的方式,详解el7.2具体调度实现.内核线程模型.调度时间片计算,以及探究整个Kernel ...

  9. 从零开始学 Web 之 Ajax(七)跨域

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  10. gitlab+jenkins=自动化构建

    jenkins:运维持续集成工具,靠着丰富的插件挑大梁. gitlab:git代码管理仓库web版,功能强大且丰富. 本文是记录自己工作中从搭建到使用~ 前提是会点git~可以去廖老师的网站学习:ww ...