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. iostat 命令详解

    前言 话说搞运维的人没有两把"刷子",都不好意思上服务器操作.还好,我还不是搞运维的,我一直都自诩是开发人员,奈何现在的东家运维人员"水"的一比,还要我这个自诩 ...

  2. 仿B站项目——(1)计划,前端工程

    计划 现打算: 计划用webpack打包 + 模板语言 + jquery + jquery ui + bootstrap做一个仿B站的静态网站. 网站兼容手机浏览器端. 部分模块打算仿照SPA用js加 ...

  3. kaili Linux学习推荐网站

    kaili学习推荐网站 kail中文网:http://www.kali.org.cn/forum.php?gid=67 Kali官方教材:https://kali.training/ Kali Lin ...

  4. USB协议介绍

    这里有必要先说清楚一下USB2.0规范的由来.USB2.0技术规范是有由Compaq.Hewlett Packard.Intel.Lucent.Microsoft.NEC.Philips共同制定.发布 ...

  5. 使用autogen生成应用程序遇到问题及解决方法

    从github上下载的代码,运行autogen.sh的时候,上报错误: $ ./autogen.sh --prefix=/usr./autogen.sh: 10: ./autogen.sh: auto ...

  6. IOS 整体框架类图值得收藏

    Cocoa框架是iOS应用程序的基础,了解Cocoa框架,对开发iOS应用有很大的帮助. 1.Cocoa是什么? Cocoa是OS X和 iOS操作系统的程序的运行环境. 是什么因素使一个程序成为Co ...

  7. vue.js多页面开发环境搭建

    利用 vue-cli 搭建的项目大都是单页面应用项目,对于简单的项目,单页面就能满足要求.但对于有多个子项目的应用,如果创建多个单页面,显示有点重复,特别是 node_modules 会有多份相同的. ...

  8. tf.nn.in_top_k原理探究

    import tensorflow as tf; A = [[0.8,0.6,0.3], [0.1,0.6,0.4],[0.5,0.1,0.9]] B = [0,2,1] out = tf.nn.in ...

  9. [每天解决一问题系列 - 0012] 如何通过程序获取IIS站点信息

    问题描述: 在WiX中需要判断某个站点是否存在,WiX没有这个能力,该怎么做呢? 解决方案: 解决方法就是写一个Custom Action来检测,实现的途径也有很多,现在想到了这么几个 1)Power ...

  10. 在Ubuntu中搭建KMS服务器

    介绍 基于vlmcsd搭建的KMS服务器. 根据github上的说明,这个工具是用C写的,没有任何依赖,可以直接运行.而且它横跨几乎现在所有的系统平台,如Android, FreeBSD, Solar ...