shell脚本--内容查找之grep命令
grep命令可以检索文件中包含关键字(可以使用正则)的行,默认区分大小写。
ubuntu@ubuntu:~/test$ cat test.txt
this is linux
this is Linux
this is mysql
this is Mysql
ubuntu@ubuntu:~/test$ grep 'linux' test.txt
this is linux
ubuntu@ubuntu:~/test$ grep 'Mysql' test.txt
this is Mysql
ubuntu@ubuntu:~/test$
使用 -c 参数,获取包含关键字的行数
ubuntu@ubuntu:~/test$ grep -c 'is' test.txt
4
ubuntu@ubuntu:~/test$ grep -c 'sql' test.txt
2
ubuntu@ubuntu:~/test$
使用 -n 参数,打印内容的同时,显示所在的行号
ubuntu@ubuntu:~/test$ cat test.txt
this is linux
this is Linux
this is mysql
this is Mysql
ubuntu@ubuntu:~/test$ grep -n 'mysql' test.txt
3:this is mysql
ubuntu@ubuntu:~/test$
使用 -i 参数,查找时,不区分大小写
ubuntu@ubuntu:~/test$ grep -i 'mysql' test.txt
this is mysql
this is Mysql
ubuntu@ubuntu:~/test$
使用 -v 参数,查找不包含关键字的行(反向查找)
ubuntu@ubuntu:~/test$ cat test.txt
this is linux
this is Linux
this is mysql
this is Mysql
ubuntu@ubuntu:~/test$ grep -v 'Linux' test.txt
this is linux
this is mysql
this is Mysql
ubuntu@ubuntu:~/test$
使用 -e 参数,可以同时指定多个筛选条件
ubuntu@ubuntu:~$ cat test.txt
this is a
this is b
three are a and b
ubuntu@ubuntu:~$ grep "a" test.txt | grep "this" #与关系,包含a,并且包含this
this is a
ubuntu@ubuntu:~$ grep -e "a" -e "this" test.txt #或关系,包含a或者包含this
this is a
this is b
three are a and b
要想使用正则表达式,可以使用 -E 参数
shell正则和perl语言的正则类似,基本通用。
ubuntu@ubuntu:~/test$ cat test.txt
this is linux
this is Linux
that are apples
ubuntu@ubuntu:~/test$ grep -E '^that' test.txt #以that开头的行
that are apples
ubuntu@ubuntu:~/test$ grep -E 'Linux$' test.txt #以Linux结尾的行
this is Linux
ubuntu@ubuntu:~/test$ grep -E '.inux' test.txt # '.'表示任意一个字符(不包含空白)
this is linux
this is Linux
ubuntu@ubuntu:~/test$ grep -E 'p*' test.txt # ‘*’表示前面一个字母出现0,1或任意多次
this is linux
this is Linux
that are apples
ubuntu@ubuntu:~/test$ grep -E '.+p.+' test.txt # ‘+’表示前面一个字母出现1或任意多次
that are apples
that are apples
ubuntu@ubuntu:~/test$ grep -E 'p{2}' test.txt # {n}前面的一个字符出现n次
that are apples
ubuntu@ubuntu:~/test$
还有一些常用的匹配模式,比如 '^$'表示一个空行 ; '^.$'表示只有一个字符的行 ; 使用 \ 来转义,比如使用\.来匹配一个点 ; [0-9]表示匹配一个数字 ; [a-z]|[A-Z]表示任意一个字母; 使用|表示‘或’ ;
ubuntu@ubuntu:~/test$ echo 'ip is 192.168.1.1' > test.txt
ubuntu@ubuntu:~/test$ grep -E '([1-9][0-9]*\.){3}[1-9][0-9]*' test.txt
ip is 192.168.1.1
ubuntu@ubuntu:~/test$
shell脚本--内容查找之grep命令的更多相关文章
- Linux Shell脚本入门--grep命令详解
grep简介<摘自鸟哥,并加以整理.> grep (global search regular expression(RE) and print out the line,全面搜索正则表达 ...
- shell脚本--文件查找之find命令
首先是通过文件名称来查找,需要使用一个-name参数. 查询以 .txt结尾的文件,和以 t 开头的文件: ubuntu@ubuntu:~/test$ ls one.txt three.txt tw ...
- Linux Shell脚本入门--wget 命令用法详解
Linux Shell脚本入门--wget 命令用法详解 wget是在Linux下开发的开放源代码的软件,作者是Hrvoje Niksic,后来被移植到包括Windows在内的各个平台上.它有以下功能 ...
- Linux输入输出重定向和文件查找值grep命令
Linux输入输出重定向和文件查找值grep命令 一.文件描述符Linux 的shell命令,可以通过文件描述符来引用一些文件,通常使用到的文件描述符为0,1,2.Linux系统实际上有12个文件描述 ...
- 如何隐藏shell脚本内容
从事 Linux 开发的同学,经常需要编写 shell 脚本,有时脚本中会涉及到一些敏感内容,比如一些 IP 地址,用户名以及密码等,或者脚本中有一些关键的代码, 所有这些内容你都不想别人阅读或者修改 ...
- Linux Shell脚本入门--cut命令
Linux Shell脚本入门--cut命令 cut cut 命令可以从一个文本文件或者文本流中提取文本列. cut语法 [root@www ~]# cut -d'分隔字符' -f fields &l ...
- shell脚本中判断上一个命令是否执行成功
shell脚本中判断上一个命令是否执行成功 shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 示例 ...
- [shell]上一个命令执行完成,才执行下一个操作 | shell脚本中判断上一个命令是否执行成功
shell脚本中判断上一个命令是否执行成功 shell中使用符号“$?”来显示上一条命令执行的返回值,如果为0则代表执行成功,其他表示失败.结合if-else语句实现判断上一个命令是否执行成功. 场 ...
- linux shell编程学习笔记(二) --- grep命令
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达 ...
随机推荐
- 【BZOJ3930】选数
[BZOJ3930]选数 Description 我们知道,从区间[L,H](L和H为整数)中选取N个整数,总共有(H-L+1)^N种方案.小z很好奇这样选出的数的最大公约数的规律,他决定对每种方案选 ...
- iptables和firewalld的配置
一.iptables 1.配置 vi /etc/sysconfig/iptables -A RH-Firewall-1-INPUT -m state --state NEW -p tcp -m tcp ...
- Spring Component注解处理过程
接下来: org.springframework.context.annotation.ComponentScanBeanDefinitionParser#parse方法展开加载过程:
- 接上篇elasticsecrchi 进行搜索及时提示,数据库以及后台代码
-- ------------------------------ Table structure for articles-- ----------------------------DROP TA ...
- VS2017 安装visualSVN 6.1.1 for visual studio 2017
1.官网下载地址:https://www.visualsvn.com/visualsvn/download/ 2.安装
- 【Codeforces 1129C】Morse Code
Codeforces 1129 C 题意:给一个0/1串,问它的每一个前缀中的每一个子串能解析成莫尔斯电码的串的种数. 思路:首先对于这个串构造后缀自动机,那么从起点走到每一个节点的每一条路径都代表了 ...
- Linux下DHCP服务安装配置
简介 安装配置 一.简介 DHCP (Dynamic Host Configuration Protocol,动态主机管理协议)是一种基于UDP协议且仅限用于局域网的网络协议,主要用途是为局域网内部设 ...
- 数据库隔离级别深入理解(ORACLE)
TRANSACTION_READ_UNCOMMITTED 1 这种隔离级别最低,脏读,不可重复读,幻读都会发生,我用的oracle,并没有支持这个级别,不作研究. TRANSACTION_READ_C ...
- kubernetes 里面pod时间修改
yaml文件中设置时区同步,只需要映射主机的“/etc/localtime”文件. apiVersion: extensions/v1beta1kind: Deploymentmetadata: na ...
- Subversion 1.8.9 ( SVN Client ) 安装最新版本的svn客户端
For CentOS7 Users: [WandiscoSVN] name=Wandisco SVN Repo baseurl=http://opensource.wandisco.com/cento ...