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. openmp并行计算

    #include <omp.h>#include <stdio.h>#include <stdlib.h> void test(int n){ for (int i ...

  2. freemodbus线圈中的位操作

    连续位的操作函数: xMBUtilSetBits(UCHAR * ucByteBuf, USHORT usBitOffset, UCHAR ucNBits,UCHAR ucValue): xMBUti ...

  3. Linux下SVN服务器安装配置及客户端安装说明

    原文地址:http://wenku.baidu.com/link?url=h3dVAMx4azpOXEND5HQEE6nliE8-zc0GSQ03yv4cUs1vXMALXF64UsK7kT7kXm_ ...

  4. Python深入05 装饰器

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 装饰器(decorator)是一种高级Python语法.装饰器可以对一个函数.方法 ...

  5. Python进阶09 动态类型

    作者:Vamei 出处:http://www.cnblogs.com/vamei 欢迎转载,也请保留这段声明.谢谢! 谢谢TeaEra, 猫咪cat 动态类型(dynamic typing)是Pyth ...

  6. spring声明式事务

    一.传统事务 二.通过spring配置完成事务: 1.配置spring,加入spring的jar包,加入spring的配置文件 2.配置数据源,这里使用c3p0,加入c3p0 jar包和mysql数据 ...

  7. inno setup 执行SQL

    参考之:1.可将导入数据的功能写入一个小程序,再外部调用(楼上已经说了):2.可用程序代码:[Setup] AppName=科發醫院管理系統 AppVerName=科發醫院管理系統4.0 AppPub ...

  8. poj 3080 Blue Jeans

    点击打开链接 Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10243   Accepted: 434 ...

  9. 在git上下载的Asp.Net MVC 4源码怎么编译?

    以本人的下载位置为例:E:\aspnetwebstack 1.win+r 输入cmd 打开dos 界面 2.e: 回车,定位到e 盘 3.cd E:\aspnetwebstack 进入e 盘aspne ...

  10. php 深入理解addslashes函数

    php addslashes函数对于很多人来说并不陌生,但很大部分人只是了解皮毛,只知道addslashes函数是在特定字符前面加上反斜杠,本文章将带大家深入理解php addslashes函数的使用 ...