正则grep

什么是正则:

正则就是一串有规律的字符串。

正则三剑客:

grep/egrep (egrep是grep的扩展),sed, awk

grep

grep过滤指定关键词

[[email protected] grep]# grep ‘nologin‘ passwd

-c 行数

[[email protected] grep]# grep -c ‘nologin‘ passwd

15

-i 不区分大小写

[[email protected] grep]# grep -i ‘nologin‘ passwd

-n 显示行号

[[email protected] grep]# grep -n ‘nologin‘ passwd

-v 取反

把除了nologin外的所有

[[email protected] grep]# grep -vin ‘nologin‘ passwd

-r遍历子目录和孙目录

[[email protected] grep]# grep -r ‘root‘ /etc/

可以把过滤到的字符串重定向到文件中,然后从文件中查找相应的字符串

[[email protected] grep]# grep -r ‘root‘ /etc/  > /tmp/grep.log

[[email protected] grep]# grep passwd /tmp/grep.log

/etc/passwd:root:x:0:0:root:/root:/bin/bash

/etc/passwd:operator:x:11:0:operator:/root:/sbin/nologin

/etc/passwd-:root:x:0:0:root:/root:/bin/bash

/etc/passwd-:operator:x:11:0:operator:/root:/sbin/nologin

/etc/postfix/main.cf:# the system passwd file in the chroot jail is just not practical.

-A 后面跟数字,过滤出符合要求的行以及下面n行

例如当前符合要求的行,以及下面的n行

[[email protected] grep]# grep  -nA2 ‘root‘ passwd

1:root:x:0:0:root:/root:/bin/bash

2-bin:x:1:1:bin:/bin:/sbin/NOLOGIN

3-daemon:x:2:2:daemon:/sbin:/sbin/nologin

--

10:operator:x:11:0:operator:/root:/sbin/nologin

11-games:x:12:100:games:/usr/games:/sbin/nologin

12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

-B 也是一样,后面跟数字,列出符合要求的当前行,以及行上面的n行

[[email protected] grep]# grep -nB2 ‘root‘ passwd

1:root:x:0:0:root:/root:/bin/bash

--

8-halt:x:7:0:halt:/sbin:/sbin/halt

9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

10:operator:x:11:0:operator:/root:/sbin/nologin

-C 也是一样,后面跟数字,列出符合要求的当前行,以及行上面n行和行后面n

[[email protected] grep]# grep -nC2 ‘root‘ passwd

1:root:x:0:0:root:/root:/bin/bash

2-bin:x:1:1:bin:/bin:/sbin/NOLOGIN

3-daemon:x:2:2:daemon:/sbin:/sbin/nologin

--

8-halt:x:7:0:halt:/sbin:/sbin/halt

9-mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

10:operator:x:11:0:operator:/root:/sbin/nologin

11-games:x:12:100:games:/usr/games:/sbin/nologin

12-ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin

显示文件中的数字(红色高亮显示数字)

[[email protected] grep]# grep ‘[0-9]‘ passwd

不显示文件中带有数字的字符

grep -v ‘[0-9]‘ passwd

-n是显示行数,-v取反,意思是不显示数字的,并且显示行数。

[[email protected] grep]# grep  -vn  ‘[0-9]‘ /etc/inittab

以井号开头的行:

‘^#‘

显示以井号开头的行

[[email protected] grep]# grep -n ‘^#‘ inittab

显示不以井号开头的行:

grep  -nv   ‘^#‘ inittab

[[email protected] grep]# grep -nv ‘^#‘ inittab

9:jhsdjhsajdhjashdj

显示非数字的字符:非数字字符高亮

[[email protected] grep]# grep ‘[^0-9]‘ inittab

显示非数字开头的行:

[[email protected] grep]# grep ‘^[^0-9]‘ inittab

^ 放到[]方括号里面意思是取反,^放到开头意思是以什么字符开头。

grep ‘r.o‘ 表示  . 点表示任意一个字符

[[email protected] grep]# grep ‘r.o‘ passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

grep ‘o*o’        * 号 表示o o中间n个字符

[[email protected] grep]# grep  ‘o*o‘ passwd

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

grep   ‘.*‘ 会匹配所有字符。    .表示任意一个字符,*表示所有字符

[[email protected] grep]# grep  ‘xiaobo.*‘ passwd

xiaobo:x:1000:1000::/home/xiaobo:/bin/bash

grep  ‘o\{2\}‘  passwd;   \表示脱义;

‘o\{2\}’表示o出现两次。

[[email protected] grep]# grep  ‘o\{2\}‘ passwd

root:x:0:0:root:/root:/bin/bash

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

‘o\{0,3\}‘ 从0到3都匹配,就是说o出现0次、1次、2次、3次;

[[email protected] grep]# grep ‘o\{0,3\}‘ passwd

去掉脱义符,用egrep ‘o{2}‘ passwd 表示o出现两次

[[email protected] grep]# egrep ‘o{2}‘ passwd

root:x:0:0:root:/root:/bin/bash

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

也可以用 grep  -E  ‘o{2}‘ passwd

[[email protected] grep]# grep  -E ‘o{2}‘ passwd

root:x:0:0:root:/root:/bin/bash

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

grep  -E ‘(oo){2}‘  passwd 表示出现4个o

[[email protected] grep]# grep  -E ‘o{4}‘ passwd

ooooqq

osdaoooo

grep    ‘o\+o‘ passwd    +加号 表示1个或者多个o

[[email protected] grep]# grep ‘o\+o‘ passwd

root:x:0:0:root:/root:/bin/bash

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

mail:x:8:12:mail:/var/spool/mail:/sbin/nologin

operator:x:11:0:operator:/root:/sbin/nologin

ooooqq

osdaoooo

postfix:x:89:89::/var/spool/postfix:/sbin/nologin

不脱义,用egrep

egrep  ‘o+o‘ passwd

epgrep  ‘o+t‘  passwd

[[email protected] grep]# egrep ‘o+t‘ passwd

root:x:0:0:root:/root:/bin/bash

operator:x:11:0:operator:/root:/sbin/nologin

egrep ‘o?t‘  passwd           ?问号表示0或1个 问号前面的字符

[[email protected] grep]# egrep ‘o?1o‘ passwd

postfix:x:89:89::/var/spool/postfix:/sbin/n1ogin

cho1orony:x:998:996::/var/lib/chrony:/sbin/nologin

grep -E   ‘root|nologin‘ passwd  匹配root或者nologin字符

[[email protected] grep]# grep -E ‘root|nologin‘ passwd

root:x:0:0:root:/root:/bin/bash

daemon:x:2:2:daemon:/sbin:/sbin/nologin

不区分大小写 -i

grep -Ei   ‘root|nologin‘ passwd  匹配root或者nologin字符

原文地址:https://www.cnblogs.com/xiaobo-Linux/p/8290274.html

时间: 2024-08-03 02:09:34

正则grep的相关文章

26期20180703 正则 grep

7月3日任务 9.1 正则介绍_grep上9.2 grep中9.3 grep下扩展把一个目录下,过滤所有*.php文档中含有eval的行grep -r --include="*.php" 'eval' /data/ 正则表达式 使用grep可以查找文件中的关键字. 语法: grep  "关键字" 文件名 有一些选项是可以选的. -n 是显示行号 -c显示行数,一共在这个文件中有多少行带有这个关键字 -v取反,就是多少行没有这个关键字,也就是除了关键字的那两行外 -A

正则grep用法

语法:grep 选项 'word' filename-c:打印符合要求的行数-i:不区分大小写-n:在输出符合要求的行的同时显示行号-v:打印不符合要求的行-A:后跟一个数字n,表示打印符合要求的行以及下面n行-B:后跟一个数字n,表示打印符合要求的行以及上面n行-C:后跟一个数字n,表示打印符合要求的行以及上下各n行-r:会把目录下所有的文件全部遍历--color:把匹配到的关键词用红色标示例子: 过滤出带有某个关键词的行并输出行号grep -n 'root' 1.txt 过滤出不带有某个关键

Linux正则grep/egrep的用法

*grep的主要作用是根据关键字检索内容,egrep是grep的拓展,egrep包含grep所有的功能 grep用法: grep '关键词' 检索对象 常用参数:-c 检索包含关键词的行数-i 不区分大小写(关键字)-n 显示行号-v 取反(显示不包含关键词的行)-r 遍历所有子目录(检索目录使用)-An 检索包含关键词的行以及下面n行-Bn 检索包含关键词的行以及上面n行-Cn 检索包含关键词的行以及上下n行-E 等于egrep 特殊用法示例: [[email protected] ~]# g

正则与sed,grep,awk三剑客

系统登录顺序: /etc/profile /etc/profile.d/a.sh (a.sh自己建的) /root/.bash_profile /root/.bashrc /etc/bashrc /bin/bash 提供命令解释器(终端) 直接打/bin/bash 非登录shell /root/.bashrc /etc/bashrc /etc/profile.d/a 可将别名alias等写入以上三个文件 正则表达式: grep -n  只显示行号 -o 只显示匹配内容 -q  安静模式,不打印

info grep半翻译加整理

内容来源基本上是整理info grep而来,当然只整理了关于用法的部分.有些地方给出了些解释,算是对grep的一个细节概述吧. 1.1 grep选项 grep家族:grep -G或者grep使用基本正则表达式:grep -E或者egrep使用扩展正则表达式:grep -F或者fgrep使用固定字符串进行匹配,不使用正则表达式进行匹配,速度很快. grep [options] pattern [file...] 1.1.1 关于匹配类型选项 -G:使用基本正则表达式,即默认的.所以一般都会省略该选

grep及正则表达式详解

grep正则表达式1.grep定义 Globally search a Regular Expression and Print 全局搜索(匹配正则表达式的)并打印 扩展命令:egrep:扩展grep,相当于grep -E 命令,使用扩展正则匹配.fgrep:fast grep,不支持正则匹配的grep,只能匹配字符本来的意义. grep与egrep的区别是egrep支持更多扩展的正则 grep参数(#代表数字)-v:显示不匹配的行-i:忽略字母大小写-o:只打印匹配的字符-E:扩展正则表达式-

第九章 正则

9.1 正则介绍 grep(上) 1)正则解释正则就是一串有规律的字符串:grep 过滤关键词< mkdir /grep/ >< cp /etc/passwd /root/grep/ >< grep 'nologin' passwd > 过滤nologin 自动标红了 < grep -c 'nologin' passwd > 显示行数<grep -n 'nologin' passwd > 显示行号< grep -ni 'nologin' p

linux shell基础(四)正则表达式与grep命令(编辑中)

一.正则表达式之前学习find命令时,就已经接触过一些简单的正则,那么我们现在来学习一些复杂的正则.首先,我们还是来复习什么是正则表达式,看这个教程先,一定要多看几遍,至少知道正则大概长什么样子.(转载至https://deerchao.net/tutorials/regex/regex.htm )正则就是有规律的一串字符串,用来描述一个特定的内容,正则适合用来处理字符串,不太适合处理数学逻辑.学好正则对编写shell脚本有很大的好处,所有的shell脚本大部分都涉及到正则表达式.各种编程工具中

SED_AWK_正则

AWK awk [选项] ‘条件{指令}’ 文件      //后面不一定要跟文件,可以接管道 命令 | awk [选项] 条件: 1,/正则/(模糊匹配) #awk ‘/root/’ /etc/passwd           //包含root的行 #awk -F: ‘$5~/root/’ /etc/passwd     //$5~代表在第5列里包含root #awk -F: ‘$5!~/root/’ /etc/passwd    //代表第5列里不包含root 2,字符和数字比较(==,!=