Linux 命令之 grep
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的更多相关文章
- 【linux命令】grep
1.作用Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局 ...
- linux命令之grep用法介绍
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- 每天一个linux命令(51)--grep命令
linux系统中grep 命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来.grep 全称是 global regular expression print,表示全局正则 ...
- 【Linux命令】grep命令
1.作用 Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全 ...
- Linux命令——find/grep
这两个命令写起来会很多,这里只简单的写一些东西,加深自己的印象. 一.find find命令主要作用是沿着文件层次结构向下遍历,匹配符合条件的文件,并执行相应的操作. 1)命令格式 find [参数] ...
- 每天学点Linux命令之grep 和 wc命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expr ession Print,表示全局正则表 ...
- 每日linux命令学习-grep模式检索
grep模式检索指令包括grep,egrep,和fgrep,.Linux系统使用正则表达式优化文本检索,所以在此,笔者首先学习了一下正则表达式. 1. 正则表达式 正则表达式使用被称为元字符(Meta ...
- linux命令系列 grep
grep, egrep, fgrep - print lines matching a pattern SYNOPSIS grep [OPTIONS] PATTERN [FILE...] grep [ ...
- 每天学点Linux命令之grep 和 wc命令 ---(6/25)
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
- linux 命令——39 grep (转)
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
随机推荐
- 随笔http
一个HTTP由一条请求命令和一个响应结果组成.这种通信是通过名为HTTP报文(message)的格式化数据块进行的.
- mysql函数计算地表两点间距离
DELIMITER $$ CREATE FUNCTION `test`.`getDistance`(LatBegin FLOAT(10,4), LngBegin FLOAT(10,4), LatEnd ...
- imageView--转-如何代码动态的获取和设置ImageView的宽度和高度?
11- 12- 13-android获取图片原始长宽; ========= 11--国内某金融企业的一道面试题:如何在代码中获得一个ImageVIew的高度和宽度? 相信很大一部分人,会直接想到用im ...
- JAVA 单选按钮、复选按钮
//单选按钮和复选按钮 import java.awt.*; import javax.swing.*; public class Jiemian6 extends JFrame{ JPanel mb ...
- 关于Objective-C 对象release操作的一个小问题探讨
来源:http://blog.csdn.net/duxinfeng2010/article/details/8757211 最近遇到这样一个问题,以前的时候并未注意:新建一个工程,然后添加一个类,文件 ...
- Delphi同步互斥总结
多个线程同时访问一个共享资源或数据时,需要考虑线程同步,Synchronize()是在一个隐蔽的窗口里运行,如果在这里你的任务很繁忙,你的主窗口会阻塞掉:Synchronize()只是将该线程的代码放 ...
- photoshop CS 调整选择区域的大小
网上看到说:矩形选框不能直接调整大小,如果你不想重新画一个可以利用转换路径,然后再调整.这是不对的,矩形选框是可以调整大小的,使用"变换选区"即可. 对应步骤截图如下: 1.画 ...
- lower_bound 和 upper_bound
Return iterator to lower bound Returns an iterator pointing to the first element in the range [first ...
- poj 2299 Ultra-QuickSort :归并排序求逆序数
点击打开链接 Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 34676 Accepted ...
- 蓝桥杯--- 历届试题 大臣的旅费 (DFS & Vector)
题目提交链接:http://lx.lanqiao.org/problem.page?gpid=T32 问题描述 很久以前,T王国空前繁荣.为了更好地管理国家,王国修建了大量的快速路,用于连接首都和王国 ...