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,表示全局正则表达 ...
随机推荐
- jQuery部分源码帮助理解
(function(window){})(window) 为什么要传window给jquery当参数呢? 1.为了压缩有 引用 2.加速变量的寻找,当找window对象的时候,默认从本级开始寻找,一级 ...
- RPC介绍
转载http://blog.csdn.net/mindfloating/article/details/39474123/ 近几年的项目中,服务化和微服务化渐渐成为中大型分布式系统架构的主流方式,而 ...
- restful api设计中的幂等性的理解。
以前就听说过这个词,但是最近看了一些文章,产生了一些疑惑, GET是幂等性.例如查一条数据,如果你给定一个id.那么你查多少次.都是这条数据,但是我疑问,这里的幂等性指的是,每次都返回一条.这个数量, ...
- springmvc 对REST风格的支持
1.PathVariable注解 用于映射url的占位符到目标方法的参数中 例子: @RequestMapping("/testPathVariable/{id}") public ...
- Java UDP网络编程 - 最简单示例
UDP也是网络通讯中的一个重要协议,与TCP区别可参见浅谈TCP/IP 和 UDP的区别,本文就对Java UDP通讯做一个简单例子介绍 服务端: package wyf; import java.i ...
- 无法重新组织表 "ty_wf_ex_local_process_info" 的索引 "idx_prc_act_id" (分区 1),因为已禁用页级锁定。
无法重新组织表 "ty_wf_ex_local_process_info" 的索引 "idx_prc_act_id" (分区 1),因为已禁用页级锁定. ALT ...
- MPlayer在ARM上的移植(S5PV210开发板)
MPlayer 1.0已经把大部分解码库都自带了,如ffmpeg,但是自带的音频库在S5PV210下效果非常不好.换成使用libmad效果不错.因此MPlayer 在ARM-Linux的最简单的移植只 ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem A
Problem A Almost Palindrome Given a line of text, find the longest almost-palindrome substring. A st ...
- MFC学习 多线程
#include <Windows.h> #include <process.h> #include <stdio.h> HANDLE hMutex; //互斥对象 ...
- JMeter二次开发(2)-编写 JSON Assertion 插件
本篇文章主要介绍如何对JMeter进行二次开发,添加自己所需的功能.这里以Json验证为例进行说明.在web接口测试过程中,JSON的应用已经非常普遍,但原声的JMeter并没有提供Json及Json ...