Linux常用基本命令:grep-从文件或者管道中筛选匹配的行
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-从文件或者管道中筛选匹配的行的更多相关文章
- Linux常用基本命令wc-统计文件的字节,字符,行数
wc命令 作用:统计文件的字节,单词,行数 用法:wc [option] [file] -c:统计字节 ghostwu@dev:~/linux/uniq$ cat ghostwu.txt 192.16 ...
- linux常用基本命令 grep awk 待优化
查看centos操作系统版本:cat /etc/centos-release 切换到当前用户主目录:cd 或者cd ~ 创建文件夹/a/b/c:mkdir -pv /a/b/c.如果/a/b/c的父目 ...
- Linux 常用基本命令及应用技巧
需要pdf 版 联系我 我的文件中有目录一.Linux 的常用基本命令................................................................. ...
- Linux常用基本命令(less)
转: Linux常用基本命令(less) LESS:跟more命令的功能类似,都是用于分页显示内容,但是他的性能比more更高,功能比more更丰富,他读取文件是按需加载 格式: less [opti ...
- linux常用基本命令
Linux中许多常用命令是必须掌握的,这里将我学linux入门时学的一些常用的基本命令分享给大家一下,希望可以帮助你们. 系统信息 arch 显示机器的处理器架构(1) uname -m 显示机器 ...
- Linux常用命令01(文件和目录)
目标 查看目录内容 ls 切换目录 cd 创建和删除文件 touch rm mkdir 拷贝和移动文件 cp mv 查看文件内容 cat more grep 其他 echo 重定向 > 和 &g ...
- linux常用基本命令整理小结
linux系统遵循的基本原则 由目标单一的小程序组成,组合小程序完成复杂任务: 一切皆文件: 尽量避免捕捉用户接口: 配置文件保存为纯文本文件: Linux命令行常识 命令格式 命令+选项+参数 选项 ...
- Linux常用命令(第二版) --文件搜索命令
文件搜索命令 1.which /usr/bin/which #显示系统命令所在目录,绝对目录,不能查找文件 格式:which [系统命令] e.g. which ls 附-whereis:也可以查找到 ...
- linux常用命令 grep命令
linux grep命令 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配行打印出来 grep 全称 Grobal Regular Expression Pr ...
随机推荐
- Linux 查看IP
UBuntu 系统下 按Ctrl+Alt+T 唤出终端 在终端输入: ifconfig 命令 点击回车 就可以看到自己电脑在局域网的IP地址了 图中第二行 inet 地址:192.168.1.101 ...
- 小程序基础知识点讲解-WXML + WXSS + JS,生命周期
小程序基础 小程序官方地址,小程序开发者工具,点击此处下载.在微信小程序中有一个配置文件project.config.json,此文件可以让开发者在不同设备中进行开发. 微信小程序共支持5种文件,wx ...
- java8 parallel并行处理实战
需求 我需要做一个人员某几项数据的统计,由于数据量较大,不能一次性加载到内存进行统计.所以采用了遍历每个用户.当然也可以分配处理. 分析需求可得知,每个用户其实互不相关,数据的统计可以同步进行,因此考 ...
- jsp页面跳转页面的几中方式(同步更新)
1. 通过<a>标签跳转网页a) 通过<a>标签直接跳转b) 通过<a>标签调用JavaScript函数,然后再进行跳转 2. 通过表单形式跳转网页a) 通过表单跳 ...
- Java的接口、继承与多态
接口 java只支持单继承,即一个类只能有一个父类,因此需要接口来实现多重继承. 接口的定义 类和接口的区别:一个类通过继承接口的方式,从而来继承接口的抽象方法.类描述对象的属性和方法,接口则包含类要 ...
- Spring.Net封闭业务类为WebService注意点和问题
最近遇到的一个项目中用到了标题所说的方法,用Spring.Net将业务类封闭成WebService供其它地方调用使用,感觉还是蛮新鲜的,于是在园子中搜了一篇园友写的文章(这里)自己也尝试着搭了一个环境 ...
- 3-2 模板语法(vue中的内容写法)
插值表达式.v-text.v-html的用法
- 转载:https原理:证书传递、验证和数据加密、解密过程解析
写的太好了,就是我一直想找的内容,看了这个对https立马明白多了 http://www.cnblogs.com/zhuqil/archive/2012/07/23/2604572.html 我们都知 ...
- Mysql插入中文的字段内容时乱码的解决方法
Mysql插入中文的字段内容时乱码的解决方法 1.我们在往数据库插入数据的时候,当遇到中文字符的插入时会发现插入的数据会变为?号,也就是乱码,如下所示: 2.这时候我们使用如下的查询语句可以看到相应的 ...
- centos下如何使用sendmail发送邮件
最近在实施服务端日志监控脚本,需要对异常情况发送邮件通知相关责任人,记录下centos通过sendmail发送邮件的配置过程. 一.安装sendmail与mail 1.安装sendmail: 1) ...