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 ? ...
随机推荐
- vim中使用sed去除网上copy的源代码行号和空格
有些时候,在网上搜索到的代码都包含有行号,高亮显示控件不支持直接提取,如: test.sh 01 #!/bin/bash 02 echo “aaa” 简单的去掉行号和前面的空格: 方案一: 1.vim ...
- ubuntu 安装 phpstorm
phpstorm是用JAVA开发的,所以在安装之前需要先安装jdksudo apt-get install default-jdk从官网上下载phpstorm 的linux版本 http://www. ...
- 经典c语言题
1. 用预处理指令#define 声明一个常数,用以表明1年中有多少秒(忽略闰年问题) #define SECONDS_PER_YEAR (60 * 60 * 24 * 365)UL 2. 写一个“标 ...
- css属性background
一.background-size 1.具体设定的px值,及北背景图片的具体宽高 2.设定的百分比,相对于容器的宽高 3.设定三个铺设类型: (1)cover: 以宽 / 高 为基本,尽可能的去铺满整 ...
- 使用deepfashion实现自己的第一个分类网络
这个过程主要分为三个步骤: 数据预处理 数据处理就是把数据按照一定的格式写出来,以便网路自己去读取数据 1准备原始数据 我的cloth数据一共是四个类别,每个类别有衣服47张,一用是188张图片,这些 ...
- Spring Cloud之Zuul负载均衡
Zuul网关默认是实现负载均衡的,不需要任何配置.默认开启ribbon效果的 可以启启动两个服务端口,访问下.
- Jackson的用法实例分析
这篇文章主要介绍了Jackson的用法实例分析,用于处理Java的json格式数据非常实用,需要的朋友可以参考下 通俗的来说,Jackson是一个 Java 用来处理 JSON 格式数据的类库,其性能 ...
- GridView行中按钮的使用
转载自:http://blog.csdn.net/hongdi/article/details/6455947 GridView行中按钮的使用 在web项目的过程中,特别是开发ASP.NET应用程序, ...
- 分享知识-快乐自己:shiro 异常类型
<!-- 身份认证异常 --> 1):身份令牌异常,不支持的身份令牌 --> org.apache.shiro.authc.pam.UnsupportedTokenException ...
- L99
You're not obligated to win. You're obligated to keep trying.你不一定要获胜,但你必须不断尝试.He announced an expans ...