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 ...
随机推荐
- IIS 设置文件传输大小限制
IIS默认传输文件大小为30M,最大允许传输为2G. 1.通过webconfig配置节点设置 在IIS 6.0 设置如下配置节点: 但是IIS 7.0-8.0还要做添加如下配置节点才能正确,否则还是默 ...
- js 基本
JavaScrip组成:1.ECMAScrip --核心2.DOM 文档对象模型3.BOOM 浏览器对象模型 JavaScrip写法分类:1.内联式写在标签内以属性为表现:2.内嵌式以script标签 ...
- Associative Containers
Notes from C++ Primer Associative containers differ in fundamental respect from the sequential conta ...
- SpringCloud总结
初级入门使用轮廓,整理一下思路
- Jedis 操作 Redis 工具类
配置类 pom.xml pom.xml 里配置依赖 <dependency> <groupId>redis.clients</groupId> <artifa ...
- Swift5 语言指南(十一) 结构和类
结构和类是通用的,灵活的结构,它们成为程序代码的构建块.您可以使用与定义常量,变量和函数相同的语法来定义属性和方法,以便为结构和类添加功能. 与其他编程语言不同,Swift不要求您为自定义结构和类创建 ...
- HTML5实现微信聊天气泡效果
最近做一个HybridApp,前端有一个群聊的功能,于是就想模仿微信的聊天界面,先看效果图: HTML代码: <!DOCTYPE html> <html lang="en& ...
- LeetCode--No.012 Integer to Roman
12. Integer to Roman Total Accepted: 71315 Total Submissions: 176625 Difficulty: Medium Given an int ...
- Autowired byType 与 byName 策略
@Autowired是spring的注解,默认使用的是byType的方式向Bean里面注入相应的Bean.例如: @Autowiredprivate UserService userService;这 ...
- 机器学习与Tensorflow(6)——LSTM的Tensorflow实现、Tensorboard简单实现、CNN应用
最近写的一些程序以及做的一个关于轴承故障诊断的程序 最近学习进度有些慢 而且马上假期 要去补习班 去赚下学期生活费 额.... 抓紧时间再多学习点 1.RNN递归神经网络Tensorflow实现程序 ...