案例介绍

搜索关键词带‘root’的行 并输出行号

[root@wangshaojun ~]# cg -n 'root' 1.txt
1:root:x:0:0:root:/root:/bin/bash
11:operator:x:11:0:operator:/root:/sbin/nologin

搜索不带关键词‘nologin’的行,输出行号

[root@wangshaojun ~]# cg -vn 'nologin' 1.txt
1:root:x:0:0:root:/root:/bin/bash
6:sync:x:5:0:sync:/sbin:/bin/sync
7:shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
8:halt:x:7:0:halt:/sbin:/sbin/halt
26:wangshaojun:x:500:500::/home/wangshaojun:/bin/bash

搜索出包含数字的行

[root@wangshaojun ~]# cg '[0-9]' 1.txt
root:x:::root:/root:/bin/bash
bin:x:::bin:/bin:/sbin/nologin
daemon:x:::daemon:/sbin:/sbin/nologin
....

搜索包含个别字符的行 ‘cN’

sync:x:5:0:sync:/sbin:/bin/sync
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin

....

搜索包含字母的行

[root@wangshaojun ~]# cg '[a-zA-Z]' 1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
....

搜索不包含字母的行

[root@wangshaojun ~]# cg -v '[a-zA-Z]' 1.txt

#$%%^&#@!!?????>>><<:"}{(特殊字符行)

(空格行)
112234556754576543   (数字行)

匹配以字母开头的行

[root@wangshaojun ~]# cg '^[a-z]' 1.txt
root:x:0:0:root:/root:/bin/bash
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

..... (没有特殊字符行,空格行,数字行)

匹配以数字开头的行

[root@wangshaojun ~]# cg '^[0-9]' 1.txt
12234556754576543

匹配不以数字开头的行

[root@wangshaojun ~]# cg -v '^[0-9]' 1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologi

(空格行)

#$%%^&#@!!?????>>><<:"}{

匹配不以数字开头的行

[root@wangshaojun ~]# cg '^[^0-9]' 1.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
nobody:x:99:99:Nobody:/:/sbin/nologin
#$%%^&#@!!?????>>><<:"}{

.....(没有空格行)

搜索空行

[root@wangshaojun ~]# cg '^$' 1.txt///// '^'是开始 '$'是结束 中间什么都没有就是空行

(空行)

////////////////////////////////////////////////////////////////////////////////////////////////

特殊符号

特殊符号‘.’  表示任意一个字符

[root@wangshaojun ~]# cg 'r.o' 1.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

vcsa:x:69:69:virtual console memory oriowner:/dev:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/r_oempty/saslauth:/sbin/nologin
postfix:x:89:89::/var/spr oool/postfix:/sbin/nologin
sshd:x:74:74:Privilege-separ*orated SSH:/var/empty/sshd:/sbin/nologin
rtkit:x:498:499:RealtimeKit: r.o/proc:/sbin/nologin

特殊符号‘*’  表示数字0个或多个*号前面的字符和*后面的字符匹配

[root@wangshaojun ~]# cg 'r.o' 1.txt
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin
vcsa:x:69:69:virtual console memory oriowner:/dev:/sbin/nologin
saslauth:x:499:76:"Saslauthd user":/var/r_oempty/sas

shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

syrrrrrrrrronc:x:5:0:sync:/sbin:/bin/sync

....

特殊字符‘.*’  .任意*任意个。表示任何个任意字符。匹配到r开头o结尾

[root@wangshaojun ~]# cg 'r.*o' 1.txt
root:x:0:0:root:/root:/bin/bash
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
syrrrrrrrrronc:x:5:0:sync:/sbin:/bin/sync
halt:x:7:rro0:halt:/sbin:/sbin/halt
mail:x:8:12:mail:/var/spool/mail:/sbin/nologin
uucp:x:10:14:uucp:/var/spool/uucp:/sbin/nologin

.....

特殊字符‘?’ 表示数字0或数字1个?前面的字符

[root@wangshaojun ~]# cg 'r\?o' 1.txt
root:x:0:0:root:/root:/bin/bash   //roo中ro匹配一次 o匹配一次
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

.......

cg 'r\?o' 1.txt == cg -E 'r?o' 1.txt == egrep --color 'r?o' 1.txt

/////////////////////////////////////////////////////////////////

shell入门-grep2的更多相关文章

  1. Linux shell入门基础(六)

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

  2. Linux shell入门基础(一)

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

  3. Shell - 简明Shell入门

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

  4. linux shell 入门

    本文是本人学习linux shell入门收集整理,不完全原创. 参考博文: http://www.cnblogs.com/suyang/archive/2008/05/18/1201990.html ...

  5. shell 入门学习

    目录 shell 入门学习 注释 执行与启动 变量 语法 调试 title: shell 入门学习 date: 2019/7/16 15:47:49 toc: true --- shell 入门学习 ...

  6. Shell入门02

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

  7. Shell入门01

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

  8. 自学linux——11.shell入门

    shell 基础 1.shell介绍(内置脚本) 程序开发的效率非常高,依赖于功能强大的命令可以迅速地完成开发任务(批处理) 语法简单,代码写起来比较轻松,简单易学 (1)什么是shell shell ...

  9. 10_Linux基础-SHELL入门1

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

随机推荐

  1. kNN算法及其python&R实现

    iris数据集,这一教科书级别的数据,分类前不需要做任何数据预处理什么的,相当的理想!但请注意你自己的数据99%的可能需要做预处理. 下面分别用R语言和Python来实现iris数据集的分类: R语言 ...

  2. python2 生成验证码图片

    使用pillow或者pil库编写 #coding:utf-8 #use pillow or pil try: from PIL import Image, ImageDraw, ImageFont, ...

  3. VS2013 Pro版本密钥

    Visual Studio Professional 2013 KEY(密钥): XDM3T-W3T3V-MGJWK-8BFVD-GVPKY

  4. 图片oom问题

    1.什么是OOM? 程序申请内存过大,虚拟机无法满足我们,然后自杀了.这个现象通常出现在大图片的APP开发,或者需要用到很多图片的时候.通俗来讲就是我们的APP需要申请一块内存来存放图片的时候,系统认 ...

  5. Swift协议+代理

    Swift语言开发中使用协议+代理的用法和oc中是一样的,只不过变得是语法.现在就进入swift的协议+代理. 先上个图,看看我们要实现的效果:  首先是第一个页面,然后点击到第二个页面,最后点击返回 ...

  6. iOS项目中获取验证码倒计时及闪烁问题解决方案

    -(void)startTime{ __block int timeout= 59; //倒计时时间 dispatch_queue_t queue = dispatch_get_global_queu ...

  7. vue双向绑定补充说明方法

    本文总结自: https://segmentfault.com/a/1190000006599500,将每一个流程提炼出来做一个简单的说明,以免自己被繁杂的逻辑弄昏头脑~ observer: 遍历数据 ...

  8. ICP 求解相机思路

    1.之前仍然是需要创建find_feature_matches,和pixel2cam,一个是用来匹配描述子的,一个是把像素坐标转成归一化平面坐标的.里面的变量都要带上&.2.因为是3d-3d. ...

  9. vRA Customizing error

    toolsDeployPkg.log An error occurred while customizing VM vwbjvuqtest0751. For details reference the ...

  10. javascript数字时钟

    <html> <head> <script type="text/javascript"> function startTime() { var ...