正则表达式,就是一个字符串。有一定的规律。我们用指定的字符串匹配一个指定的行。指定的字符串就是正则表达式。

正则表达式有这几个工具: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的更多相关文章

  1. Shell入门01

    Shell入门 1.基于硬件的虚拟化 2.基于平台的虚拟化 3.基于服务的虚拟化 4.基于库的虚拟化 5.基于操作系统的虚拟化 管理员使用Shell程序与操作系统进行交互,之前学习的shell脚本都是 ...

  2. Linux shell入门基础(六)

    六.Shell脚本编程详解 将上述五部分的内容,串联起来,增加对Shell的了解 01.shell脚本 shell: # #perl #python #php #jsp 不同的脚本执行不同的文本,执行 ...

  3. grep 过滤.svn文件

    [grep 过滤.svn文件] 问题: 在repository搜索代码时,常常会搜索到.svn的代码,如果不想搜索.svn目录下的相关代码怎么办?    1.使用管道进行双层“过滤”,其中第二次gre ...

  4. Shell入门02

    Shell入门-02 1.重定向 标准输入(<) 标准输出 标准错误重回定向 程序 = 指令 + 数据 命令   变量 在程序中,数据如何输入?又如何输出? 数据输入:键盘 – 标准输入,但是并 ...

  5. 10_Linux基础-SHELL入门1

    @ 目录 10_Linux基础-SHELL入门1 一. 输入输出重定向 二. 2个特殊文件 三. here document 四. tee命令 五. 清空文件内容 六. SHELL入门 SHELL的变 ...

  6. 使用 powershell 的 grep 过滤文本

    使用 powershell 的 grep 过滤文本 有个log文件,大小在4M左右,要求找出里面耗时超过100s 的记录.首先想到了强大的 grep ,那么就搞起. 先在网上找一下资料,这篇文章,有几 ...

  7. Linux shell入门基础(一)

    Linux shell入门基础(一): 01.增加删除用户: #useradd byf   userdel byf(主目录未删除)  userdel -r byf   该用户的属性:usermod 用 ...

  8. Shell - 简明Shell入门

    本文以示例和注释的方式,对Shell编程的基本知识点进行了总结和回顾,所有脚本均已做了基本的调试和验证. Shell - 简明Shell入门 01 - 第一个脚本 脚本的定义.执行方法以及echo命令 ...

  9. [20170828]grep过滤技巧.txt

    [20170828]grep过滤技巧.txt --//经常使用grep过滤显示信息. #  ps -ef |grep oraagentoracle    13416      1  0  2016 ? ...

随机推荐

  1. SAP内表转XML文件

    今天有个兄弟问如何实现以XML的方式输出内表的内容,这个问题我以前好像没有写过.倒不是不会写,而是写的方法太多了,有极其简单的,也有很复杂的,而且网上资料也很多. 找到以前写的一个程序,稍微修改了一下 ...

  2. PAT 天梯赛 L2-010. 排座位 【并查集】

    题目链接 https://www.patest.cn/contests/gplt/L2-010 思路 因为 题意中 朋友的朋友 就是朋友 那么 朋友的关系 用 并查集 保存 但是 敌对关系 只有直接的 ...

  3. 《python基础教程(第二版)》学习笔记 基础部分(第1章)

    <python基础教程(第二版)>学习笔记 基础部分(第1章)python常用的IDE:Windows: IDLE(gui), Eclipse+PyDev; Python(command ...

  4. php中session的配置

    在 php.ini 中搜  session.save_path 查看session文件保存的目录;

  5. SSAS——基础

    一.Analysis Services Analysis Services是用于决策支持和BI解决方案的数据引擎.它提供报表和客户端中使用的分析数据. 它可在多用途数据模型中创建高性能查询结构,业务逻 ...

  6. 分享知识-快乐自己:PageHelper 分页关键基础代码

    关键代码:点我下载样式   详细介绍 //使用分页插件 jQuery("#pagination").pagination(data.total, { items_per_page: ...

  7. adb命令(一)

    针对移动端 Android 的测试, adb 命令是很重要的一个点,必须将常用的 adb 命令熟记于心, 将会为 Android 测试带来很大的方便,其中很多命令将会用于自动化测试的脚本当中. And ...

  8. Selenium-几种元素定位方式

    #识别元素并操作#一般有如下几种方法,其中id最为常用.这里需要注意识别元素一定要用唯一id 1.find_element_by_id("value") #! /usr/bin/e ...

  9. 机器学习 Support Vector Machines 3

    Optimal margin classifiers 前面我们讲过,对如下的原始的优化问题我们希望找到一个优化的边界分类器. minγ,w,bs.t.12∥w∥2y(i)(wTx(i)+b)⩾1,i= ...

  10. 【leetcode刷题笔记】N-Queens II

    Follow up for N-Queens problem. Now, instead outputting board configurations, return the total numbe ...