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 ? ...
随机推荐
- 搜索ABAP程序代码中的字符串
标准程序名:RPR_ABAP_SOURCE_SCAN /BEV1/NERM07DOCS
- js判断undefined类型,undefined,null, 的区别详细解析
js判断undefined类型 今天使用showModalDialog打开页面,返回值时.当打开的页面点击关闭按钮或直接点浏览器上的关闭则返回值是undefined所以自作聪明判断 var reVal ...
- Data Structure Trie: suffix problem
http://www.geeksforgeeks.org/suffix-array-set-1-introduction/ http://www.geeksforgeeks.org/pattern-s ...
- 剑指offer——扑克牌的顺子
思想: 1.先将输入的几个数进行排序,sort函数是#include<algorithm>下的. 2.统计0的个数,以及相邻数的差值,比较0的个数及差值的和.看是否可以用大王填充中间的差值 ...
- MySQL服务器调优思路
1.mysqladmin -uroot ext|awk '/Queries/{q=$4}/Threads_connected/{c=$4}/Threads_running/{r=$4}END{prin ...
- LUA学习之一 初次接触
对于一个开源工程,开始学习它的第一步自然是编译工程. 使用vc编译lua,在网上已有许多介绍,但“纸上得来终觉浅”,自己走一遍还是有必要的. 步骤如下: 1.下载源代码,从lua.org下载最新源代码 ...
- L106 Three things we learned from day one at the World Cup
Hosts Russia got the World Cup off to a flying start by hammering Saudi Arabia 5-0 in the opening ga ...
- python遍历并获取对象属性--dir(),__dict__,getattr,setattr
一.遍历对象的属性: 1.dir(obj) :返回对象的所以属性名称字符串列表(包括属性和方法). for attr in dir(obj): print(attr) 2.obj.__dict__:返 ...
- deepmoji:文本预测emoji
输入句子,预测emoji demo: https://deepmoji.mit.edu/ github: https://github.com/bfelbo/DeepMoji 能够被预测的emoji ...
- html中Meta属性
<!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --> <html lang="zh-cmn-Hans"&g ...