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. 第三周Access的总结

    一.问;这节课你学到了什么知识? 答:这周我学得比较少,主要是学Access的数据库进行基本的维护. 2.3数据库的基本维护 对Access定期检查,修复是整个数据库重要部分: 1.Access可修复 ...

  2. ESP32作为接入点AP

    作为接入点的第一个任务是使用esp_wifi_set_mode()函数设置ESP32 函数并传递请求,作为接入点,可以把ESP32设置为AP或者APSTA,即 esp_wifi_set_mode(WI ...

  3. python学习第五章

    1.继承 即是一个派生的类(derived class)继承基类(base class)的字段和方法,继承也允许把一个 派生类的对象作为 一个基类 对象对待.通俗来讲就是方便,继承前人的代码,减少工作 ...

  4. EBS CAS SSO测试

    https://wiki.jasig.org/display/CAS/CASifying+Oracle+Portal https://wenku.baidu.com/view/5f110a85b9d5 ...

  5. Visual Studio 2017 调试器的工作进程(msvsmon.exe)意外退出 调试将终止

    开发环境: Windows 10 in Parallels Desktop Visual Studio 15.6 场景还原: 使用 Visual Studio 15.6 (即 Visual Studi ...

  6. Swift5 语言指南(六) 字符和字符串

    甲串是一系列字符,如的或.Swift字符串由类型表示.可以通过各种方式访问a的内容,包括作为值的集合."hello, world""albatross"Stri ...

  7. Python模块——logging模块

    logging模块简介 logging模块定义的函数和类为应用程序和库的开发实现了一个灵活的事件日志系统.logging模块是Python的一个标准库模块, 由标准库模块提供日志记录API的关键好处是 ...

  8. IOS返回go(-1)

    IOS8和9,在用go(-1)返回的时候,会同时加载js.可能会造成js加载顺序出错,或者值被覆盖的情况,我们可以用setTimeout(function(){XXX代码},100);延时加载.

  9. Djang--module--单表

    Django模型层   一 ORM简介 查询数据层次图解:如果操作mysql,ORM是在pymysq之上又进行了一层封装

  10. 使用Let's Encrypt加密你的小站

    介绍 Let's Encrypt是一个免费并且开源的CA,且已经获得Mozilla.微软等主要浏览器厂商的根授信.它极大低降低DV证书的入门门槛,进而推进全网的HTTPS化. Certbot is a ...