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. iOS 批量打包--Shell脚本

    由于公司有批量打包的需要,只是渠道号不一样,网上搜索一番,大致有两个方式:a) 从源程序一次性打出所有渠道的ipa包,大概的思路就是利用脚本循环执行打包过程,而每次打包前都通过脚本修改项目中存放渠道号 ...

  2. 更换ubuntu apt-get 源

    为了优化ubuntu软件安装/更新速度,我测试了国内几家apt源的速度,发现北京交大的apt源速度相对最快,然后可以通过以下步骤更新ubuntu源 1) 备份默认的apt源 $ cd /etc/apt ...

  3. 在JVM发生FGC前后dump内存

    原文  http://itindex.net/detail/46666-jvm-fgc-dump 一.需求 有时候web应用经常会发生FGC,我们想知道FGC把那些对象给回收了,思路很简单就是看看FG ...

  4. /dev/shm

    /dev/shm/是linux下一个特殊的目录,因为这个目录不在硬盘上,而是在内存里. /dev /shm/需要注意的一个是容量问题,在linux下,它默认最大为内存的一半大小,使用df -h命令可以 ...

  5. 卸载oracle

    1.   开始->设置->控制面板->管理工具->服务   停止所有Oracle服务.    2.   开始->程序->Oracle   -   OraHome81 ...

  6. OpenCV数据结构:CvMat,IplImage,CvArr的应用

    最近看HS算法时,发现在新的OpenCV3.0已经移除了该算法,于是不得不看老版的.这其中涉及到了CvMat,CvArr,IplImage的使用.关于这三个结构与Mat的转换关系,总结如下: (1)派 ...

  7. [SQL]获取所有数据库名、获取数据库中表名、获取表中的字段名

    --()获取所有数据库名: Select Name FROM Master..SysDatabases order by Name --()获取所有表名 --XType=''U'':表示所有用户表; ...

  8. nyoj 85 有趣的数

    点击打开链接 有趣的数 时间限制:3000 ms  |  内存限制:65535 KB 难度: 描述 把分数按下面的办法排成一个数表. 1/1 1/2 1/3 1/4..... 2/1 2/2 2/3. ...

  9. jQuery学习笔记----入门

    基础语法是:$(selector).action() 美元符号定义 jQuery 选择符(selector)“查询”和“查找” HTML 元素 jQuery 的 action() 执行对元素的操作

  10. (转)C#精确时间计时器

    原文地址:http://blog.sina.com.cn/s/blog_699d3f1b01012vgb.html 1 调用WIN API中的GetTickCount [DllImport(" ...