shell入门-grep过滤-1
正则表达式,就是一个字符串。有一定的规律。我们用指定的字符串匹配一个指定的行。指定的字符串就是正则表达式。
正则表达式有这几个工具:grep egrep sed awk
命令:gerep
说明:过滤出指定的行
选项:--color 关键字有颜色
-n 显示行号
-c 显示一共出现了多少行
-v 取反 不包含指定字符的行
-A n n指数字 例如A2在有指定字符的行下面再显示两行
-B n n指数字 例如B2 在有指定字符的行上面再显示两行
-C n n指数字 例如C2 在有指定字符的行上面和下面再显示各两行
-r 显示目录里的所以带指定字符的行
-rh 显示目录里的所以带指定字符的行并不显示文件路径和文件名
grep 过滤出有root的行
[root@wangshaojun ~]# grep --color 'root' /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
grep -n
[root@wangshaojun ~]# grep -n 'root' /etc/passwd
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin
给grep创建个别名,把/etc/passwd拷贝一下
[root@wangshaojun ~]# vim .bashrc
alias cg=‘grep --color’
[root@wangshaojun ~]# cp /etc/passwd 1.txt
cp:是否覆盖"1.txt"? y
-c
[root@wangshaojun ~]# cg -c 'root' 1.txt
2
-v
[root@wangshaojun ~]# cg -v 'root' 1.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
....
-A
[root@wangshaojun ~]# cg -n -A 2 'root' 1.txt
1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/nologin
3-daemon:x:2:2:daemon:/sbin:/sbin/nologin
--
11:operator:x:11:0:operator:/root:/sbin/nologin
12-games:x:12:100:games:/usr/games:/sbin/nologin
13-gopher:x:13:30:gopher:/var/gopher:/sbin/nologin
-B
[root@wangshaojun ~]# cg -n -B1 'root' 1.txt
1:root:x:0:0:root:/root:/bin/bash
--
10-uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
11:operator:x:11:0:operator:/root:/sbin/nologin
-C
[root@wangshaojun ~]# cg -n -C1 'root' 1.txt
1:root:x:0:0:root:/root:/bin/bash
2-bin:x:1:1:bin:/bin:/sbin/nologin
--
10-uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
11:operator:x:11:0:operator:/root:/sbin/nologin
12-games:x:12:100:games:/usr/games:/sbin/nologin
-r
[root@wangshaojun ~]# cg -r 'root' ~
.....
/root/.bash_history:HOME=/root
/root/.bash_history:grep 'root' /etc/passwd
/root/.bash_history:grep -n 'root' 1.txt
/root/.bash_history:grep -c 'root' 1.txt
/root/.bash_history:grep -v 'root' 1.txt
/root/.bash_history:grep -A2 'root' 1.txt
/root/.bash_history:grep -n -A2 'root' 1.txt
/root/.bash_history:grep -n -b2 'root' 1.txt
/root/.bash_history:grep 'root' /etc/passwd
/root/.bash_history:grep -n 'root' /etc/passwd
/root/.bash_history:cg -c 'root' 1.txt
.......
[root@wangshaojun ~]# cg -rh 'root' ~
....
HOME=/root
grep 'root' /etc/passwd
grep -n 'root' 1.txt
grep -c 'root' 1.txt
grep -v 'root' 1.txt
grep -A2 'root' 1.txt
grep -n -A2 'root' 1.txt
grep -n -b2 'root' 1.txt
grep 'root' /etc/passwd
grep -n 'root' /etc/passwd
cg -c 'root' 1.txt
......
//////////////////////////////////////////////////////////////////////////
总结:vim ~/.bashrc /// alias cg=‘grep --color’
cg -n -v -c -r -rh -An -Bn -Cn
shell入门-grep过滤-1的更多相关文章
- Shell入门01
Shell入门 1.基于硬件的虚拟化 2.基于平台的虚拟化 3.基于服务的虚拟化 4.基于库的虚拟化 5.基于操作系统的虚拟化 管理员使用Shell程序与操作系统进行交互,之前学习的shell脚本都是 ...
- Linux shell入门基础(六)
六.Shell脚本编程详解 将上述五部分的内容,串联起来,增加对Shell的了解 01.shell脚本 shell: # #perl #python #php #jsp 不同的脚本执行不同的文本,执行 ...
- grep 过滤.svn文件
[grep 过滤.svn文件] 问题: 在repository搜索代码时,常常会搜索到.svn的代码,如果不想搜索.svn目录下的相关代码怎么办? 1.使用管道进行双层“过滤”,其中第二次gre ...
- Shell入门02
Shell入门-02 1.重定向 标准输入(<) 标准输出 标准错误重回定向 程序 = 指令 + 数据 命令 变量 在程序中,数据如何输入?又如何输出? 数据输入:键盘 – 标准输入,但是并 ...
- 10_Linux基础-SHELL入门1
@ 目录 10_Linux基础-SHELL入门1 一. 输入输出重定向 二. 2个特殊文件 三. here document 四. tee命令 五. 清空文件内容 六. SHELL入门 SHELL的变 ...
- 使用 powershell 的 grep 过滤文本
使用 powershell 的 grep 过滤文本 有个log文件,大小在4M左右,要求找出里面耗时超过100s 的记录.首先想到了强大的 grep ,那么就搞起. 先在网上找一下资料,这篇文章,有几 ...
- Linux shell入门基础(一)
Linux shell入门基础(一): 01.增加删除用户: #useradd byf userdel byf(主目录未删除) userdel -r byf 该用户的属性:usermod 用 ...
- Shell - 简明Shell入门
本文以示例和注释的方式,对Shell编程的基本知识点进行了总结和回顾,所有脚本均已做了基本的调试和验证. Shell - 简明Shell入门 01 - 第一个脚本 脚本的定义.执行方法以及echo命令 ...
- [20170828]grep过滤技巧.txt
[20170828]grep过滤技巧.txt --//经常使用grep过滤显示信息. # ps -ef |grep oraagentoracle 13416 1 0 2016 ? ...
随机推荐
- 特性,批次特性建立的BAPI函數
[转http://taijizhang.blog.163.com/blog/static/176071381201442225514453/] SAP特性,物料特性,批次特性建立的BAPI函數 类的T ...
- (转)Web Service和WCF的到底有什么区别
[1]Web Service:严格来说是行业标准,也就是Web Service 规范,也称作WS-*规范,既不是框架,也不是技术. 它有一套完成的规范体系标准,而且在持续不断的更新完善中. 它使用XM ...
- linux 11 -- mount,umount
Linux 文件系统是一个以 / 为根的大树,我们在不同的设备和分区上都有文件系统.我们如何处理这种明显的不一致性?根 (/) 文件系统是在初始化过程中挂载的.您创建的其他每个文件系统在挂载 在挂载点 ...
- linux shell 字符串操作(长度,查找,替换)
感谢原创,文章很有帮助. 转自:http://www.cnblogs.com/chengmo/archive/2010/10/02/1841355.html 一.判断读取字符串值 表达式 含义 ${v ...
- (转)Spring 缓存EhCacheFactoryBean
Spring使用Cache 从3.1开始,Spring引入了对Cache的支持.其使用方法和原理都类似于Spring对事务管理的支持.Spring Cache是作用在方法上的,其核心思想是这样的:当我 ...
- 从性能角度分析一下String,List,Map
使用String.subString()方法的时候注意内存溢出的问题 public static void testH() { List<String> strings = new Arr ...
- ios中文模糊搜索兼容问题
$(function(){ var cpLock = true; $("input[name='name']").off().on({ compositionstart: func ...
- Luogu-4196 [CQOI2006]凸多边形
凸多边形的面积就相当于半平面交 求几个凸多边形面积交就相当于一堆半平面一起求交 #include<cmath> #include<cstdio> #include<cst ...
- 《python基础教程(第二版)》学习笔记 类和对象(第7章)
<python基础教程(第二版)>学习笔记 类和对象(第7章) 定义类class Person: def setName(self,name): self.name=n ...
- 算法(Algorithms)第4版 练习 2.2.5
top-down: M E R G E S O R T E X A M P L E M E R G E S O R T E X A M P L E E X A M P L E merge(input, ...