RH134-01 配合grep使用正则表达式

第二章、配合grep使用正则表达式

2.1 正则表达式基础

介绍shell中的常用正则表达式

^   以什么开头             ^#

$   以什么结尾              y$

.   匹配任意一个字符

.*匹配0个或若干个字符

h*匹配0个h或若干个h

h+匹配1个或更多个h

h?匹配0个或1个h

h{2}  匹配 hh (两个hh)

[abc]匹配a或b或c

[a-z]匹配所有的小写字符

[A-Z]匹配大写字母

[a-Z]匹配所有字符

[0-9]匹配所有数字

练习:匹配 IP地址的格式,但无需判断IP是否合理。格式要求满足 以 "."分割是四组数字,每组数字可以1~3位数

0.0.0.0- 255.255.255.255

[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}

2.2、使用grep匹配数据

使用grep和正则表达式过滤文件内容和需要的日志内容

练习:建立一个文本/tmp/cats.txt,文本内容如下

cat

caaat

catdog

cat2dog

catanddog

dogcat

ccat

catdogcccc

c123t

c45678t

Cat

cAt

catdogDogCAT

#this is a cat

;this is a dog

$ grep "cat"/tmp/cats.txt

$ grep -i "cat"/tmp/cats.txt  忽略大小写

$ grep  ^cat /tmp/cats.txt

$ grep  dog$ /tmp/cats.txt

$ grep ^catdog$  /tmp/cats.txt

$ grep ^cat.*dog$  /tmp/cats.txt

$ grep ^cat.dog$  /tmp/cats.txt

$ grep ^cat...dog$  /tmp/cats.txt

$ grep -E^cat.{3}dog$  /tmp/cats.txt  中间3个任意字符

$ grep ^c[0-9]*t$/tmp/cats.txt  [0-9]*  匹配0个或若干个数字

$ grep -E^"[#;]" /tmp/cats.txt 以# ;开头的

$ grep -e ^"#"-e ^";" /tmp/cats.txt  作用同上,-e 可以通过指定多个表达式

练习1:过去日志,把 August 8 sometime between 1:00pm and 3:00pm 时间段的日志找到

http://classroom.example.com/pub/materials/awesome_logs/door.log

$ grep "Aug  8 1[34]"  door.log

=========================================================================

2.2

上课笔记

2.2

[[email protected] tmp]$ cat cats.txt

t

catdog

cat2dog

catanddog

dogcat

ccat

catdogcccc

c123t

c45678t

Cat

cAt

catdogDogCAT

#this is a cat

;this is a dog

[[email protected] tmp]$

[[email protected] tmp]$

[[email protected] tmp]$ grep"cat" /tmp/cats.txt

[[email protected] tmp]$ grep"cat" /tmp/cats.txt

catdog

cat2dog

catanddog

dogcat

ccat

catdogcccc

catdogDogCAT

#this is a cat

[[email protected] tmp]$ grep -i"cat" /tmp/cats.txt

catdog

cat2dog

catanddog

dogcat

ccat

catdogcccc

Cat

cAt

catdogDogCAT

#this is a cat

[[email protected] tmp]$

[[email protected] tmp]$ grep ^cat/tmp/cats.txt

catdog

cat2dog

catanddog

catdogcccc

catdogDogCAT

[[email protected] tmp]$

可以对要找的内容加双 引号,也可以不加

[[email protected] tmp]$ grep ^cat /tmp/cats.txt

catdog

cat2dog

catanddog

catdogcccc

catdogDogCAT

[[email protected] tmp]$ ^C

[[email protected] tmp]$ grep"^cat" /tmp/cats.txt

catdog

cat2dog

catanddog

catdogcccc

catdogDogCAT

[[email protected] tmp]$

[[email protected] tmp]$ grep dog$/tmp/cats.txt

catdog

cat2dog

catanddog

;this is a dog

[[email protected] tmp]$ grep ^catdog$/tmp/cats.txt

catdog

[[email protected] tmp]$

[[email protected] tmp]$ grep ^cat.*dog$/tmp/cats.txt

catdog

cat2dog

catanddog

[[email protected] tmp]$

.*0个或 若干个其他字符

[[email protected] tmp]$ grep ^cat.dog$/tmp/cats.txt

cat2dog

[[email protected] tmp]$

.表示一个字符

[[email protected] tmp]$ grep -E^cat.{3}dog$ /tmp/cats.txt

catanddog

[[email protected] tmp]$ grep ^cat...dog$/tmp/cats.txt

catanddog

[[email protected] tmp]$

以上两种相同,前者显得更专业

[[email protected] tmp]$  grep ^c[0-9]*t$ /tmp/cats.txt

c123t

c45678t

ct也可以匹配

$ grep -E^"[#;]" /tmp/cats.txt 以# ;开头的

[[email protected] tmp]$ grep -E^"[#;]" /tmp/cats.txt

#this is a cat

;this is a dog

[[email protected] tmp]$ ifconfig | grep[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}

对输出结果进行查找

练习:

先下载 下来

[[email protected] tmp]$ wget -O/tmp/door.log  http://classroom.example.co

m/pub/materials/awesome_logs/door.log

--2017-06-25 12:22:01-- http://classroom.example.com/pub/materials/awesome_logs/door.log

Resolving classroom.example.com(classroom.example.com)... 172.25.254.254

Connecting to classroom.example.com(classroom.example.com)|172.25.254.254|:80... connected.

HTTP request sent, awaiting response... 200OK

Length: 58722 (57K) [text/plain]

Saving to: ‘/tmp/door.log’

100%[======================================>]58,722      --.-K/s   in 0.001s

2017-06-25 12:22:02 (43.6 MB/s) -‘/tmp/door.log’ saved [58722/58722]

[[email protected] tmp]$

grep -E "Aug  8 1[34]" /tmp/door.log

Aug 8之间是两个空格。

考题:

Grep  “UUID” /etc/fstab  > /tmp/find.txt

时间: 2024-08-06 22:50:58

RH134-01 配合grep使用正则表达式的相关文章

grep及正则表达式 含(含断言)

grep和正则表达式 1.grep:Global search REgular expression and Print out the file 作用:文本搜索工具,根据用户指定的模式对目标文件逐行进行匹配,打印匹配到的行 模式:由正则表达式字符及文本字符编写的过滤条件 格式: grep [options] pattern file[...] --color=auto 对匹配到的文本着色显示 -v 显示不能够被pattern匹配的行 -i 忽略file中文件字符的大小写 -n 显示匹配的行号

grep与正则表达式,grep、egrep和fgrep

grep用法详解:grep与正则表达式 首先要记住的是: 正则表达式与通配符不一样,它们表示的含义并不相同!正则表达式只是一种表示法,只要工具支持这种表示法, 那么该工具就可以处理正则表达式的字符串.vim.grep.awk .sed 都支持正则表达式,也正是因为由于它们支持正则,才显得它们强大:1基础正则表达式grep 工具,以前介绍过.grep -[acinv]   '搜索内容串'   filename-a 以文本文件方式搜索-c 计算找到的符合行的次数-i 忽略大小写-n 顺便输出行号-v

[Linux 006]——grep和正则表达式

在使用系统时,我们或多或少的有一些搜索.查找的需求,必须要在文本中搜索某个关键字,或者过滤出文本中某些特定的行.grep 命令就为我们提供了这样一个功能,同时,grep 还可以使用正则表达式进行匹配,这是一个强大的功能,有必要好好掌握. 1.grep 初体验 grep PATTERN [OPTIONS] FILE:在文件中按照模式进行查找.FILE 是我们要查找的目标文件,如果不指定目标文件,grep 将会从标准输入中读取输入的内容,然后进行匹配.为了方便起见,本文的所有演示都在命令行中通过标准

初识grep及正则表达式

很多时候我们都有文本检索的需要,比如我们使用windows的时候,经常会在某个doc或txt文档中搜索自己想要的内容.而在使用linux的过程中,我们同样会遇到文本检索的场景. 在linux环境下,我们使用grep,egrep及fgrep,来实现文本检索的功能.由于grep,egrep支持使用正则表达式来表示匹配模式,使得linux环境下的文本检索功能变得非常强大.下面我们就来详细描述下何为grep,以及使用正则表达式配合grep实现文本检索的方法. 一,什么事正则表达式? 很难用比较通俗的语言

grep用法详解 grep与正则表达式

Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expression Print,表示全局正则表达式版本,它的使用权限是所有用户. 主要参数    [options]主要参数:    -c:只输出匹配行的计数.    -I:不区分大 小写(只适用于单字符).    -h:查询多文件时不显示文件名.    -l:查询多文件时只输出包含匹配字符的文件名.    -n:显示匹配行及 行号.    -s

学习之路(三)浅谈:输出重定向,grep及正则表达式,egrep

地址总线:内存寻址 数据总线:传输数据 控制总线:控制指令 > :输出重定向(会覆盖原有内容) >>: 追加重定向(不会覆盖,追加输出) 2>: 重定向错误输出 2>>: 错误追加输出 &>: 重定向标准输出或错误输出至同一文件 <: 输入重定向 <<: 此处生成文档 管道: 前一个命令的输出作为后一个命令的输入 tee: 不仅输出信息同时保存指定目录下的文件 grep及正则表达式 grep:根据模式搜索文本,并将符合模式文本行显示出来

Linux &nbsp; &nbsp; grep命令,正则表达式

grep命令及正则表达式 grep命令 grep , egrep , fgrep grep,sed,awk 文本处理三剑客 grep: Global search REgular expression and Print out the line;全面查找正则表达式并将匹配到的行显示出来; 正则表达式 正则表达式主要应用对象是文本,因此它在各种文本编辑器场合都有应用;许多程序设计语言都支持利用正则表达式进行字符串操作; 主流的正则引擎又分为三类:DFA;传统型NFA;POSIX NFA; DFA

grep及正则表达式随笔

grep [acivn] [--color=auto] '查找字符串' filename -a:将binary文件以text文件的方式查找数据 -c:计算找到'查找字符串'的次数 -i:忽略大小写 -v:反向选择 -n:将查找结果列出行号 --color=auto:查找结果中关键字着色 **********grep使用例子********** (1)列出一个目录下目录的名称: ll |grep '^d' |awk '{print $9}' 其中awk中$后面是列的位置 (2)列出一个文件,去除空

grep与正则表达式

一.grep简介 二.基本正则表达式 三.egrep扩展正则表达式 四.其他文本及查看工具{wc,cut,sort,uniq,diff,patch} 五.练习用例 一.grep简介 文本处理工具: Linux上文本处理三剑客: grep:文本过滤工具(模式:pattern) grep:基本正则表达式,-E扩展,-F egrep:扩展正则表达式,-G基本,-F fgrep:不支持正则表达式, sed:stream editor,流 编辑器 awk:Linux上实现的为Gawk,GNU/awk,文本