1:将/etc/passwd中有root字符的行显示出来

 [root@bogon zkero]# grep -n 'root' /etc/passwd
:root:x:::root:/root:/bin/bash
:operator:x:::operator:/root:/sbin/nologin

2:指定文件(test1)中读取hello 无论大小写的行

 [root@bogon zkero]# grep -in 'hello' test1
:hello
:HELLO

说明:grep 中‘hello’中的'hello'大小写也是忽略的

3:定位符的使用^

/etc/passwd 中只显示首次出现root的行

 [root@bogon zkero]# grep -n 'root' /etc/passwd
:root:x:::root:/root:/bin/bash
:operator:x:::operator:/root:/sbin/nologin
[root@bogon zkero]# grep -n '^root' /etc/passwd
:root:x:::root:/root:/bin/bash

^在[ ]之内是反向选择,在[ ]之外是定位符

4:显示行尾字符所在行

 [root@bogon zkero]# grep -n 'bash$' /etc/passwd
:root:x:::root:/root:/bin/bash
:zkero:x:::zkero:/home/zkero:/bin/bash

5:定位显示空白行

 [root@bogon zkero]# cat -n test1
hello
world
HELLO WORLD i an supker
i like computer! [root@bogon zkero]# grep -n '^$' test1
:
:
:

6:一般脚本文件都有空白行和#注释开头的行,如果特殊需要,需要省略这些行:

 [root@bogon zkero]# cat /etc/sysctl.conf
# Kernel sysctl configuration file for Red Hat Linux
#
# For binary values, is disabled, is enabled. See sysctl() and
# sysctl.conf() for more details. # Controls IP packet forwarding
net.ipv4.ip_forward = # Controls source route verification
net.ipv4.conf.default.rp_filter = # Do not accept source routing
net.ipv4.conf.default.accept_source_route = # Controls the System Request debugging functionality of the kernel
kernel.sysrq = # Controls whether core dumps will append the PID to the core filename.
# Useful for debugging multi-threaded applications.
kernel.core_uses_pid = # Controls the use of TCP syncookies
net.ipv4.tcp_syncookies = # Disable netfilter on bridges.
net.bridge.bridge-nf-call-ip6tables =
net.bridge.bridge-nf-call-iptables =
net.bridge.bridge-nf-call-arptables = # Controls the default maxmimum size of a mesage queue
kernel.msgmnb = # Controls the maximum size of a message, in bytes
kernel.msgmax = # Controls the maximum shared segment size, in bytes
kernel.shmmax = # Controls the maximum number of shared memory segments, in pages
kernel.shmall =
[root@bogon zkero]# grep -v '^$' /etc/sysc
sysconfig/ sysctl.conf
[root@bogon zkero]# grep -v '^$' /etc/sysctl.conf | grep -v '^#'
net.ipv4.ip_forward =
net.ipv4.conf.default.rp_filter =
net.ipv4.conf.default.accept_source_route =
kernel.sysrq =
kernel.core_uses_pid =
net.ipv4.tcp_syncookies =
net.bridge.bridge-nf-call-ip6tables =
net.bridge.bridge-nf-call-iptables =
net.bridge.bridge-nf-call-arptables =
kernel.msgmnb =
kernel.msgmax =
kernel.shmmax =
kernel.shmall =

7: 限定连续RE字符范围{ } 

找两个连续的o字符

 [zkero@bogon ~]$ cat test2
mygod!
good morning!
eat food!
gooooodbye! [zkero@bogon ~]$ grep -n 'o\{2\}' test2
:good morning!
:eat food!
:gooooodbye!

其中:{ } 为特殊符号,需要用\转义

查找连续字符后面接特定字符:比如:3到5个连续字符o后面接d

 [zkero@bogon ~]$ grep -n 'o\{3,5\}d' test2
:gooooodbye!

查找g开头,d结尾,指定o数目的例子:

 [zkero@bogon ~]$ grep -n 'go\{2,5\}d' test2
:good morning!
:gooooodbye!
[zkero@bogon ~]$ grep -n 'go\{2,4\}d' test2
:good morning!

可知:第4行有5个o,指定2到4个范围,并没有出现第4行内容

Linux 命令之 grep的更多相关文章

  1. 【linux命令】grep

    1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局 ...

  2. linux命令之grep用法介绍

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...

  3. 每天一个linux命令(51)--grep命令

    linux系统中grep 命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.grep 全称是 global regular expression print,表示全局正则 ...

  4. 【Linux命令】grep命令

    1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全 ...

  5. Linux命令——find/grep

    这两个命令写起来会很多,这里只简单的写一些东西,加深自己的印象. 一.find find命令主要作用是沿着文件层次结构向下遍历,匹配符合条件的文件,并执行相应的操作. 1)命令格式 find [参数] ...

  6. 每天学点Linux命令之grep 和 wc命令

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expr ession Print,表示全局正则表 ...

  7. 每日linux命令学习-grep模式检索

    grep模式检索指令包括grep,egrep,和fgrep,.Linux系统使用正则表达式优化文本检索,所以在此,笔者首先学习了一下正则表达式. 1. 正则表达式 正则表达式使用被称为元字符(Meta ...

  8. linux命令系列 grep

    grep, egrep, fgrep - print lines matching a pattern SYNOPSIS grep [OPTIONS] PATTERN [FILE...] grep [ ...

  9. 每天学点Linux命令之grep 和 wc命令 ---(6/25)

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...

  10. linux 命令——39 grep (转)

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...

随机推荐

  1. alpha融合

    //alpha融合 //作者:sandy //时间:2015-10-6 //将一只狗的头像融合在蜗牛头上 #include <cv.h> #include <highgui.h> ...

  2. vs2015编译boost 64位

    ---恢复内容开始--- step 1: 打开Developer Command Prompt for VS2015命令行窗口 step 2: 执行bootstrap.bat,产生bjam.exe s ...

  3. JAVA toString方法

    在JAVA中,所有的对象都有toString方法: 创建类时没有定义toString方法,输出对象时,会输出对象的哈希值: 它只是sun公司开发java的时候为了方便所有类的字符串操作而特意加入的一个 ...

  4. dedecms v5.7 sp1 给栏目添加缩略图功能

    一.给数据库添加字段typeimg 如图:   二 . 修改 dede/catalog_add.php 找到 $in_query = "INSERT INTO `#@__arctype`(r ...

  5. memcached命令行参数说明(转)

    1.启动Memcache 常用参数 -p <num>      设置TCP端口号(默认不设置为: 11211) -U <num>      UDP监听端口(默认: 11211, ...

  6. tony_linux下网站压力测试工具webbench

    webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好,安装使用也特别方便. 1.适用系统:Linux 2.编译安装:wget http:// ...

  7. 老师你好。使用cordova生成的hellowold 的安卓5.0版本太高。怎么才可以生成4.4的呢?

    你好 在你的应用目录,有个config.xml文件,课程没有介绍每个配置项.你可以增加一项 preference name="android-targetSdkVersion" v ...

  8. 九度OJ1085

    说起这个题呢,就不得不提一种快速求解幂的算法——反复平方法,可以在O(logn)的复杂度完成求幂运算.具体思路我不说,巫泽俊大神翻译的<挑战程序设计竞赛>P123对此有详细描述. 但仅知道 ...

  9. 小知识:如何解压cpio.gz文件

    第一种方法: zcat xxxx.cpio.gz | cpio -idmv 第二种方法 :第一步: gunzip xxxx.cpio.gz第二步:cpio -idmv < xxxx.cpio # ...

  10. Regional Changchun Online--Elven Postman(裸排序二叉树)

    Elven Postman Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Tot ...