shell文本过滤编程(一):grep和正则表达式【转】
转自:http://blog.csdn.net/shallnet/article/details/38799739
版权声明:本文为博主原创文章,未经博主允许不得转载。如果您觉得文章对您有用,请点击文章下面“顶”。 【版权声明:转载请保留出处:blog.csdn.NET/gentleliu。Mail:shallnew at dot com】 Linux系统中有很多文件,比如配置文件、日志文件、用户文件等。文件中都包含了大量的信息,我们可以使用cat等命令轻松将其输出到屏幕,但如果要从文件中分析或提取数据,还需要其他工具来实现。而linux正好提供了这些工具:grep、awk、sed等。把这些工具使用好,可以极大地提高你的工作效率,对系统管理员分析数据有极大帮助,而对linux开发人员来说也可以在开发测试中以及平时使用中节省不少时间。本系列文中将介绍这些工具来实现文本过滤分析。 常用的grep选项有:
-c 只输出匹配行的计数。
-i 不区分大小写(只适用于单字符) 。
-h 查询多文件时不显示文件名。
-l 查询多文件时只输出包含匹配字符的文件名。
-n 显示匹配行及行号。
-s 不显示不存在或无匹配文本的错误信息。
-v 显示不包含匹配文本的所有行。
本节文章大数示例均使用文件/etc/passwd来作为过滤对象。
一、匹配行
最简单(也是最常用)的用法是在一个文件(或多个文件)中查找某一字符串,比如在文件/etc/passwd中查找包含字符串”user”的行:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep "user" /etc/passwd
usbmuxd:x:::usbmuxd user:/:/sbin/nologin
oprofile:x:::Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
qemu:x:::qemu user:/:/sbin/nologin
radvd:x:::radvd user:/:/sbin/nologin
tss:x:::Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
saslauth:x:::"Saslauthd user":/run/saslauthd:/sbin/nologin
rpcuser:x:::RPC Service User:/var/lib/nfs:/sbin/nologin
nm-openconnect:x:::NetworkManager user for OpenConnect:/:/sbin/nologin
# 一般我们将要查找的字符串用双引号包含起来,一是以防被误解为 s h e l l命令,二是可以用来查找多个单词组成的字符串。
使用-c选项输出匹配行数:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep -c "user" /etc/passwd # 使用-n选项输出匹配行及行号:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep -n "user" /etc/passwd
:usbmuxd:x:::usbmuxd user:/:/sbin/nologin
:oprofile:x:::Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
:qemu:x:::qemu user:/:/sbin/nologin
:radvd:x:::radvd user:/:/sbin/nologin
:tss:x:::Account used by the trousers package to sandbox the tcsd daemon:/dev/null:/sbin/nologin
:saslauth:x:::"Saslauthd user":/run/saslauthd:/sbin/nologin
:rpcuser:x:::RPC Service User:/var/lib/nfs:/sbin/nologin
:nm-openconnect:x:::NetworkManager user for OpenConnect:/:/sbin/nologin
# 其中第一列输出行号,后面输出行内容。
我们发现上面匹配“user”的行有的返回的是包含“rpcuser”和“trousers“的行,如果我们要精确地抽取“user”的行,可以这样用:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep -n "\<user\>" /etc/passwd
:usbmuxd:x:::usbmuxd user:/:/sbin/nologin
:oprofile:x:::Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
:qemu:x:::qemu user:/:/sbin/nologin
:radvd:x:::radvd user:/:/sbin/nologin
:saslauth:x:::"Saslauthd user":/run/saslauthd:/sbin/nologin
:nm-openconnect:x:::NetworkManager user for OpenConnect:/:/sbin/nologin
# 瞬间少了27和34行。
如果要忽略大小写的话可以使用-i选项,如下:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 [root@localhost shell_text_filter]# grep -ni "\<user\>" /etc/passwd
:ftp:x:::FTP User:/var/ftp:/sbin/nologin
:polkitd:x:::User for polkitd:/:/sbin/nologin
:usbmuxd:x:::usbmuxd user:/:/sbin/nologin
:oprofile:x:::Special user account to be used by OProfile:/var/lib/oprofile:/sbin/nologin
:colord:x:::User for colord:/var/lib/colord:/sbin/nologin
:qemu:x:::qemu user:/:/sbin/nologin
:radvd:x:::radvd user:/:/sbin/nologin
:saslauth:x:::"Saslauthd user":/run/saslauthd:/sbin/nologin
:rpcuser:x:::RPC Service User:/var/lib/nfs:/sbin/nologin
:nfsnobody:x:::Anonymous NFS User:/var/lib/nfs:/sbin/nologin
:nm-openconnect:x:::NetworkManager user for OpenConnect:/:/sbin/nologin 瞬间多了几行。
使用选项-v可以过滤不含指定字符串的行:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep -v "a" /etc/passwd
bin:x:::bin:/bin:/sbin/nologin
sync:x:::sync:/sbin:/bin/sync
shutdown:x:::shutdown:/sbin:/sbin/shutdown
nobody:x:::Nobody:/:/sbin/nologin
polkitd:x:::User for polkitd:/:/sbin/nologin
usbmuxd:x:::usbmuxd user:/:/sbin/nologin
unbound:x:::Unbound DNS resolver:/etc/unbound:/sbin/nologin
qemu:x:::qemu user:/:/sbin/nologin
openvpn:x:::OpenVPN:/etc/openvpn:/sbin/nologin
tcpdump:x::::/:/sbin/nologin
# 上面列出的行都不包含”a”。
结合使用ps命令可以查找当前系统是否正在运行我们需要的程序,命令如下:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # ps x | grep vsftpd
? Ss : /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
pts/ S+ : grep --color=auto vsftpd
# 但是发现打印结果包含当前shell进程,我们可以通过使用-v选项来过滤输出结果,如下:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # ps x | grep "vsftpd" | grep -v "grep"
? Ss : /usr/sbin/vsftpd /etc/vsftpd/vsftpd.conf
# 该方法相当常用。
二、结合使用正则表达式
其实grep过滤可以和正则表达式联合起来用,使匹配加入一些规则,这样的使用更为灵活。使用正则表达式时最好用单引号括起来,这样可以防止 g r e p中使用的专有模式与一些shell命令的特殊方式相混淆。
这里我们先讲一下正则表达式,下面是正则表达式基本元字符集及其含义:
^ 只只匹配行首
$ 只只匹配行尾
* 只一个单字符后紧跟*,匹配0个或多个此单字符
[ ] 只匹配[ ] 内字符。可以是一个单字符,也可以是字符序列。可以使用 -表示[ ]内字符序列范围,如用 [ - ]代替[ ]
\ 只用来屏蔽一个元字符的特殊含义。因为有时在 s h e l l中一些元字符有
特殊含义。\可以使其失去应有意义
. 只匹配任意单字符
p a t t e r n \ { n \ } 只用来匹配前面 p a t t e r n出现次数。n为次数
p a t t e r n \ { n,\ } m 只含义同上,但次数最少为 n
p a t t e r n \ { n,m \ } 只含义同上,但 p a t t e r n出现次数在n与m之间。
句点“.”可以匹配任意单字符。“.”允许匹配A S C I I集中任意字符,或为字母,或为数字。
比如:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep '.mm..' /etc/passwd
smmsp:x::::/var/spool/mqueue:/sbin/nologin
#
# grep 'm..l' /etc/passwd
mail:x:::mail:/var/spool/mail:/sbin/nologin
mailnull:x::::/var/spool/mqueue:/sbin/nologin
# 在行首以^匹配字符串或字符序列,^只允许在一行的开始匹配字符或单词。
比如匹配以ma开头的行:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep '^ma' /etc/passwd
mail:x:::mail:/var/spool/mail:/sbin/nologin
mailnull:x::::/var/spool/mqueue:/sbin/nologin
# 正则表达式可以将各种模式混合使用:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep '^ma....ll' /etc/passwd
mailnull:x::::/var/spool/mqueue:/sbin/nologin
# ^在正则表达式中使用频繁,因为大量的抽取操作通常在行首。
在行尾以$匹配字符串或字符, $与^正相反,它在行尾匹配字符串或字符, $符号放在匹配单词后。比如匹配以bash结尾的行:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep 'bash$' /etc/passwd
root:x:::root:/root:/bin/bash
allen:x:::allen:/home/allen:/bin/bash
aln:x::::/home/aln:/bin/bash
# 如果要匹配所有空行,执行以下操作:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 ^ $ 具体分析为匹配行首,又匹配行尾,中间没有任何模式,因此为空行。
如果只返回包含一个字符的行,操作如下:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 ^ . $ 不像空白行,在行首与行尾之间有一个模式,代表任意单字符。
使用*匹配字符串中的单字符或其重复序列,使用此特殊字符匹配任意字符或字符串的重复多次表达式。
使用[]匹配一个范围或集合,可以用逗号将括弧内要匹配的不同字符串分开,使用“ - ”表示一个字符串范围,表明字符串范围从“ -”左边字符开始,到“ - ”右边字符结束。
比如要匹配包含aln或all的行,可以如下写:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep 'al[l,n]' /etc/passwd
allen:x:::allen:/home/allen:/bin/bash
aln:x::::/home/aln:/bin/bash
# 忽略大小写的也可以由另外一种方式:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep 'System' /etc/passwd
dbus:x:::System message bus:/:/sbin/nologin
pulse:x:::PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
[root@localhost shell_text_filter]# grep '[Ss]ystem' /etc/passwd
dbus:x:::System message bus:/:/sbin/nologin
systemd-journal-gateway:x:::Journal Gateway:/var/log/journal:/usr/sbin/nologin
pulse:x:::PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
# 使用\{\}匹配模式结果出现的次数,使用*可匹配所有匹配结果任意次,但如果只要指定次数,就应使用 \ { \ } ,此模式有三种形式,即:
pattern\{n\} 匹配模式出现n次。
pattern\{n,\} 匹配模式出现最少 n次。
pattern\{n,m} 匹配模式出现n到m次之间,n , m为0 - 5中任意整数。
过滤字符m至少出现2次的行:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep 'm\{2,\}' /etc/passwd
smmsp:x::::/var/spool/mqueue:/sbin/nologin
# 过滤9出现2次,并以4结尾的行:
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep '9\{2,\}4' /etc/passwd
pulse:x:::PulseAudio System Daemon:/var/run/pulse:/sbin/nologin
saslauth:x:::"Saslauthd user":/run/saslauthd:/sbin/nologin
# grep命令加-E参数,这一扩展允许使用扩展模式匹配。比如要获取包含allen或aln的行,可以这样用。
[html] view plain copy
在CODE上查看代码片派生到我的代码片 # grep -E 'allen|aln' /etc/passwd
allen:x:::allen:/home/allen:/bin/bash
aln:x::::/home/aln:/bin/bash
#
shell文本过滤编程(一):grep和正则表达式【转】的更多相关文章
- shell文本过滤编程(一):grep和正則表達式
[版权声明:转载请保留出处:blog.csdn.net/gentleliu.Mail:shallnew at 163 dot com] Linux系统中有非常多文件,比方配置文件.日志文件.用户文件等 ...
- shell文本过滤编程(十一):paste命令
[版权声明:转载请保留出处:blog.csdn.net/gentleliu. Mail:shallnew at 163 dot com] 从字面上能够看出.paste命令和cut命令功能相反,cut命 ...
- 第5天(半天)【shell编程初步、grep及正则表达式】
第5天(半天)[shell编程初步.grep及正则表达式] shell编程初步(01)_recv shell脚本:文本文件 #!:/bin/bash #!:/usr/bin/python #!:/us ...
- 4.1 shell文本过滤、find、awk、grep
文本过滤涉及到以下知识: 正则表达式.find.grep.awk.sed.合并与分割(sort.uniq.join.cut.paste.split). 正则表达式: 基本元字符集及其含义: 匹配IP地 ...
- Linux Shell编程第3章——正则表达式
目录 正则表达式基础 正则表达式的扩展 通配 grep命令 正则表达式基础 Linux Shell以字符串作为表达式向系统传达意思.元字符(Metacharacters)是用来阐述字符表达式意义的字符 ...
- Linux系统编程(17)——正则表达式进阶
C的变量和Shell脚本变量的定义和使用方法很不相同,表达能力也不相同,C的变量有各种类型,而Shell脚本变量都是字符串.同样道理,各种工具和编程语言所使用的正则表达式规范的语法并不相同,表达能力也 ...
- SHELL脚本编程的常识和VI常用技巧
来源:http://mprc.pku.edu.cn/mentors/training/TrainingCourses/material/ShellProgramming.HTM#_Toc3751808 ...
- 浅谈自底向上的Shell脚本编程及效率优化
作者:沐星晨 出处:http://blog.csdn.net/sosodream/article/details/6276758 浅谈自底向上的Shell脚本编程及效率优化 小论文,大家多批评指导:) ...
- Linux文本处理三剑客之grep及正则表达式详解
Linux文本处理三剑客之grep及正则表达式详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Linux文本处理三剑客概述 grep: 全称:"Global se ...
随机推荐
- php订单号的生成
来自ECSHOP订单号生成函数:/includes/lib_order.php文件中的get_order_sn() /** * 得到新订单号 * @return string */ function ...
- 动态规划:HDU1224-Free DIY Tour
Free DIY Tour Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- [BZOJ1208]宠物收养所(Splay)
Description 最近,阿Q开了一间宠物收养所.收养所提供两种服务:收养被主人遗弃的宠物和让新的主人领养这些宠物.每个领养者都希望领养到自己满意的宠物,阿Q根据领养者的要求通过他自己发明的一个特 ...
- [Poj1273]Drainage Ditches(网络流)
Description 给图,求最大流 最大流模板题,这里用dinic Code #include <cstdio> #include <cstring> #include & ...
- SpringCloud 微服务一:spring boot 基础项目搭建
spring cloud是建立在spring boot的基础上的,而之前虽然听说过,也随便看了一下spring boot,却没有真正使用,因此还必须先花时间学一下spring boot. spring ...
- 37、iamgeview 图层叠加
1 Drawable d1 = new BitmapDrawable(circleBitmap); Drawable d2 = login.this.getResources().getDrawabl ...
- 【Pow(x,n)】
题目: Implement pow(x, n). 代码: class Solution { public: double myPow(double x, int n) { double ret = S ...
- Python+Selenium练习篇之13-获取当前页面的URL
本文介绍如何通过webdriver方法获取当前测试页面的URL.获取当前URL有什么用处呢,一般URL可以帮助我们判断跳转的页面是否正确,或者URL中部分字段可以作为我们自动化测试脚本期待结果的一部分 ...
- nodejs、yarn编译安装
yarn和npm一样,是nodejs的一个依赖管理工具 1.安装nodejs 如果缺少c++ compiler 会报错 yum install -y gcc gcc-c++ 安装nodejs V8 ...
- import from 相对路径
项目目录 - server - static - src - - stroe - - router - - main.js - app.js src为前端文件,src目录下有main.js代码如下 i ...