运维SRE-02 正则表达式、grep

2024-02-02 17:36

本文主要是介绍运维SRE-02 正则表达式、grep,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.特殊符号补充

1.1位置相关的特殊符号

. 当前目录
.. 当前目录的上级目录
~ 当前用户家目录
/ 根目录
cd - 返回上次所在目录

1.2熟练掌握

# 注释符号,root命令提示符
| 管道符号.

1.3了解其他特殊符号

$ 取值(取出变量的值),普通用户的提示符
!
%
^
&
*
()
+
=
{}
[]
;
?
\

2.通配符

* :*.txt  *.log  *.avi
{} :
通配符
*所有,任何内容
{}生成序列
?任意一个字符
[]
[^]
@基本用法
[root@hb-sre-001 find]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@hb-sre-001 find]# echo {01..10}
01 02 03 04 05 06 07 08 09 10
[root@hb-sre-001 find]# echo oldboy{01..10}
oldboy01 oldboy02 oldboy03 oldboy04 oldboy05 oldboy06 oldboy07 oldboy08 oldboy09 oldboy10
[root@hb-sre-001 find]# echo {a..z}
a b c d e f g h i j k l m n o p q r s t u v w x y z
[root@hb-sre-001 find]# echo {1..10}
1 2 3 4 5 6 7 8 9 10
[root@hb-sre-001 find]# echo {1,5,9}
1 5 9
[root@hb-sre-001 find]# echo oldboy{1,5,9}
oldboy1 oldboy5 oldboy9
[root@hb-sre-001 find]# #通过{}进行备份
[root@hb-sre-001 find]# echo oldboy{,.bak}
oldboy oldboy.bak
[root@hb-sre-001 find]# echo a{b,c}
ab ac#不常用
[root@hb-sre-001 find]# echo {1..10..2}
1 3 5 7 9

?任意一个字符

[root@hb-sre-001 find]# ls /bin/?
/bin/[  /bin/w
[root@hb-sre-001 find]# ll /bin/??
-rwxr-xr-x. 1 root root  52640 5月  30 22:01 /bin/ab
-rwxr-xr-x. 1 root root  62680 10月  2 2020 /bin/ar
-rwxr-xr-x. 1 root root 386424 10月  2 2020 /bin/as
-rwxr-xr-x. 1 root root  83424 6月  10 2014 /bin/bc
-rwxr-xr-x. 1 root root     26 4月   1 2020 /bin/bg
-rwxr-xr-x. 1 root root     26 4月   1 2020 /bin/cd
-rwxr-xr-x. 1 root root 155176 8月  20 2019 /bin/cp
-rwxr-xr-x. 1 root root  45392 6月  10 2014 /bin/dc
-rwxr-xr-x. 1 root root  74896 8月  20 2019 /bin/dd
-rwxr-xr-x. 1 root root 105016 8月  20 2019 /bin/df
-rwxr-xr-x. 1 root root 112992 8月  20 2019 /bin/du
lrwxrwxrwx. 1 root root      2 8月   1 00:47 /bin/ex -> vi
-rwxr-xr-x. 1 root root     26 4月   1 2020 /bin/fc
-rwxr-xr-x. 1 root root     26 4月   1 2020 /bin/fg
-rwxr-xr-x. 1 root root  37400 8月  20 2019 /bin/id
lrwxrwxrwx. 1 root root     20 8月   1 00:47 /bin/ld -> /etc/alternatives/ld
-rwxr-xr-x. 1 root root  58592 8月  20 2019 /bin/ln
-rwxr-xr-x. 1 root root 117608 8月  20 2019 /bin/ls
-rwxr-xr-x. 1 root root 130360 8月  20 2019 /bin/mv
lrwxrwxrwx. 1 root root     22 8月   2 10:56 /bin/nc -> /etc/alternatives/nmap
-rwxr-xr-x. 1 root root  41576 8月  20 2019 /bin/nl
-rwxr-xr-x. 1 root root  42392 10月  2 2020 /bin/nm
-rwxr-xr-x. 1 root root  66368 8月  20 2019 /bin/od
-rwxr-xr-x. 1 root root  66672 8月  20 2019 /bin/pr
-rwxr-xr-x. 1 root root 100112 10月  1 2020 /bin/ps
-rwxr-xr-x. 3 root root  76760 6月  10 2014 /bin/rb
-rwxr-xr-x. 1 root root  62872 8月  20 2019 /bin/rm
-rwxr-xr-x. 3 root root  76760 6月  10 2014 /bin/rx
-rwxr-xr-x. 3 root root  76760 6月  10 2014 /bin/rz
-rwxr-xr-x. 3 root root  80872 6月  10 2014 /bin/sb
lrwxrwxrwx. 1 root root      6 8月   1 00:47 /bin/sg -> newgrp
lrwxrwxrwx. 1 root root      4 8月   1 00:47 /bin/sh -> bash
-rwsr-xr-x. 1 root root  32128 10月  1 2020 /bin/su
-rwxr-xr-x. 3 root root  80872 6月  10 2014 /bin/sx
-rwxr-xr-x. 3 root root  80872 6月  10 2014 /bin/sz
-rwxr-xr-x. 1 root root  45680 8月  20 2019 /bin/tr
-rwxr-xr-x. 1 root root  19936 10月  1 2020 /bin/ul
-rwxr-xr-x. 1 root root 928056 10月 14 2020 /bin/vi
-rwxr-xr-x. 1 root root  41648 8月  20 2019 /bin/wc
-rwxr-xr-x. 1 root root  75280 11月  5 2016 /bin/xz

正则表达式

目标:掌握常见的正则表达式符号,通过三剑客使用

正则表达式:

啥是正则?
学习正则注意事项?
正则VS通配符?
学习正则+grep+http://re.oldboylinux.cn/#!flags=&re=%5E(a%7Cb)*%3F%24
习题,

学习正则注意事项
  1. 刚开始学习,建议通过正则网站和grep一起理解与观察,正则符号含义与特点。
  2. 正则符号都是英文符号
  3. 给grep命令设置好别名,过滤的时候有红色(C7)
正则vs通配符
共同点支持的命令匹配的内容不同
正则用过过滤字符.三剑客,高级语言python,GoLang,java…过滤字符(文件内容)
通配符让人感觉是用于过滤字符.Linux下面大部分命令都可以使用匹配文件名
正则概述
分类
基础正则^ $ . * [] [^]
扩展正则| () {} ? +
基础正则-详解

环境准备 re.txt

I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com  
our size is http://blog.oldboyedu.com  
my qq is 49000448
not 4900000448.
my god ,i am not oldbey,but OLDBOY!

1)^以…开头的行

#案例1:
过滤以my开头的行
[root@hb-sre-001 hb]# grep '^my' re.txt 
my blog is http://oldboy.blog.51cto.com  
my qq is 49000448
my god ,i am not oldbey,but OLDBOY!

2)$以…结尾的行

案例2:过滤出以数字8结尾的行
[root@hb-sre-001 hb]# grep '8$' re.txt 
my qq is 49000448
案例3:过滤出以m结尾的行
[root@hb-sre-001 hb]# grep 'm$' re.txt 
[root@hb-sre-001 hb]# 
#说明文件结尾的地方可能还有特殊符号,无法看到.
#cat -A查看即可.
[root@hb-sre-001 hb]# cat -A re.txt 
I am oldboy teacher!$
I teach linux.$
I like badminton ball ,billiard ball and chinese chess!$
my blog is http://oldboy.blog.51cto.com M-BM- $
our size is http://blog.oldboyedu.com M-BM- $
my qq is 49000448$
not 4900000448.$
my god ,i am not oldbey,but OLDBOY!$

在这里插入图片描述

3)^$空行(这行这行没有任何字符)

案例4 过滤出文件中的空行并显示行号.
[root@hb-sre-001 hb]# grep -n '^$' re.txt 
3:
7:
10:案例5 排除文件中的空行(遇到空行不显示)
grep -nv '^$' re.txt
[root@hb-sre-001 hb]# grep -nv '^$' re.txt
1:I am oldboy teacher!
2:I teach linux.
4:I like badminton ball ,billiard ball and chinese chess!
5:my blog is http://oldboy.blog.51cto.com  
6:our size is http://blog.oldboyedu.com  
8:my qq is 49000448
9:not 4900000448.
11:my god ,i am not oldbey,but OLDBOY!

4).(点)任意一个字符

案例06:过滤任意一个字符的行,就把空行过虑掉了
[root@hb-sre-001 hb]# grep '.' re.txt 
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com ?
our size is http://blog.oldboyedu.com ?
my qq is 49000448
not 4900000448.
my god ,i am not oldbey,but OLDBOY!案例7:过滤出文件中以.结尾的行
转义字符\,让.就只有.的意思,而不是任意字符的意思
[root@hb-sre-001 hb]# grep '\.$' re.txt 
I teach linux.
not 4900000448.

转义字符系列:(echo -e支持转义字符)

转义字符系列
\n回车
\ttab

6)* 前一个字符,连续出现了0次或0次以上

案例8 过滤出连续出现的0
[root@hb-sre-001 hb]# grep '0*' re.txt 
I am oldboy teacher!
I teach linux.I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com ?
our size is http://blog.oldboyedu.com ?my qq is 49000448
not 4900000448.my god ,i am not oldbey,but OLDBOY!
[root@hb-sre-001 hb]# grep -o '0*' re.txt 
000
00000
核心

理解:连续出现
理解:贪婪性

7).* 所有,任何内容

案例09: 过滤文件以任意字符开头一直到http的行
[root@hb-sre-001 hb]# grep '0*' re.txt 
I am oldboy teacher!
I teach linux.I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com ?
our size is http://blog.oldboyedu.com ?my qq is 49000448
not 4900000448.my god ,i am not oldbey,but OLDBOY!
[root@hb-sre-001 hb]# grep -o '0*' re.txt 
000
00000
[root@hb-sre-001 hb]# grep '^.*http' re.txt 
my blog is http://oldboy.blog.51cto.com ?
our size is http://blog.oldboyedu.com ?案例10:理解贪婪性  过滤出文件以任意字符开头一直到o(字母的行)
[root@hb-sre-001 hb]# grep '^.*o' re.txt 
I am oldboy teacher!
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com  
our size is http://blog.oldboyedu.com  
not 4900000448.
my god ,i am not oldbey,but OLDBOY!这里把好几个o(不是第一个o结尾)结尾的都过滤出来了,验证了贪婪性

8)[] [abc] 匹配a或b或c,表示1个整体,相当于是1个字符

[root@hb-sre-001 hb]# grep '[abc]' re.txt 
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com ?
our size is http://blog.oldboyedu.com ?
my god ,i am not oldbey,but OLDBOY!案例11 过滤文件中的小写字母
[root@hb-sre-001 hb]# grep '[a-z]' re.txt 
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com  
our size is http://blog.oldboyedu.com  
my qq is 49000448
not 4900000448.
my god ,i am not oldbey,but OLDBOY!案例12 过滤文件中的小写字母或大写字母
[root@hb-sre-001 hb]# grep '[a-zA-Z]' re.txt 
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com  
our size is http://blog.oldboyedu.com  
my qq is 49000448
not 4900000448.
my god ,i am not oldbey,but OLDBOY!案例13 过滤文件中的小写字母或大写字母或数字
[root@hb-sre-001 hb]# grep '[a-zA-Z0-9]' re.txt 
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com  
our size is http://blog.oldboyedu.com  
my qq is 49000448
not 4900000448.
my god ,i am not oldbey,but OLDBOY!

在这里插入图片描述
在这里插入图片描述

可以看到grep的执行逻辑是一个字符一个字符过滤,所以会出现一起匹配的情况

案例14  过滤出以8或.或!结尾的行
[root@hb-sre-001 hb]# grep '[8.!]$' re.txt 
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my qq is 49000448
not 4900000448.
my god ,i am not oldbey,but OLDBOY!

在这里插入图片描述

9)[^] [^abc]过滤出不是a或b或c的内容。一个整体相当于是1个字符

案例15   不匹配文件中的小写字母
[root@hb-sre-001 hb]# grep '[^a-z]' re.txt 
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com  
our size is http://blog.oldboyedu.com  
my qq is 49000448
not 4900000448.
my god ,i am not oldbey,but OLDBOY!

10)小结基础正则

基础正则符号
^以…开头的行
$以…结尾的行
^$空行
.任意一个字符
.*所有
[][abc] a或b或c
[^][^abc] 取反的 排除a或b或c
|转义字符

3.6扩展正则-详解

扩展正则需要使用:egrep或grep -E、/sed -r /awk

1)| 或者

案例16 取出文件中包含oldboy或blog的行
[root@hb-sre-001 hb]# grep -E 'oldboy|blog' /hb/re.txt 
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com ?
our size is http://blog.oldboyedu.com ?[root@hb-sre-001 hb]# egrep 'oldboy|blog' re.txt 
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com  
our size is http://blog.oldboyedu.com  扩展办法:
[root@hb-sre-001 hb]# grep 'oldboy\|blog' re.txt 
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com  
our size is http://blog.oldboyedu.com  ##注意:
[]     里面的内容都是独立的[oldboy]匹配o或l或d....建议:匹配单个字符
|      可以是独立也可以整体   a|b|c    oldboy|lidao 建议:匹配整个单词

2)+前一个字符,连续出现1次或1次以上
工作建议:一般与[]搭配。

案例  连续出现的0
[root@hb-sre-001 hb]# grep -E '0+' re.txt 
my qq is 49000448
not 4900000448.案例17   取出文件中连续出现的数字
[root@hb-sre-001 hb]# egrep '[0-9]+' re.txt 
my blog is http://oldboy.blog.51cto.com  
my qq is 49000448
not 4900000448.案例18: 取出文件中的单词
(1)单词  连续出现字母
(2)字母[a-Z]
(3)连续 +
[root@hb-sre-001 hb]# egrep '[a-Z]+' re.txt 
I am oldboy teacher!
I teach linux.
I like badminton ball ,billiard ball and chinese chess!
my blog is http://oldboy.blog.51cto.com  
our size is http://blog.oldboyedu.com  
my qq is 49000448
not 4900000448.
my god ,i am not oldbey,but OLDBOY!

3)() 被括起来的内容是个整体:表示反向引用(后向引用),一般应用在sed中

案例  熟悉  过滤出oldboy或oldbey的行
egrep 'oldboy|oldbey' re.txt
[root@hb-sre-001 hb]# egrep 'oldb(o|e)y' re.txt 
I am oldboy teacher!
my blog is http://oldboy.blog.51cto.com  
our size is http://blog.oldboyedu.com  
my god ,i am not oldbey,but OLDBOY!

4){} o{n,m}前一个字符o连续出现至少n次,最多m次
o{n}前一个字符o连续出现n次
o{n,}前一个字符o连续出现至少n次
o{,m}前一个字符o连续出现最多m次

案例20 
[root@hb-sre-001 hb]# egrep '0{1,4}' re.txt 
my qq is 49000448
not 4900000448.
[root@hb-sre-001 hb]# egrep '0{3}' re.txt 
my qq is 49000448
not 4900000448.
[root@hb-sre-001 hb]# egrep '0{4}' re.txt 
not 4900000448.
[root@hb-sre-001 hb]# egrep '0{3,4}' re.txt 
my qq is 49000448
not 4900000448.

5)? 前一个字符出现0次或1次

[root@hb-sre-001 hb]# egrep 'gd|god' wenhao.txt 
gd
god
[root@hb-sre-001 hb]# egrep 'go?d' wenhao.txt 
gd
godo出现0次,就是gd
o出现1次,就是god
扩展正则
|或者
+连续出现1次或1次以上
()整体;后向引用
{}前一个字符连续出现n次到m次
?前一个字符出现0次或1次

命令大全

在这里插入图片描述

这篇关于运维SRE-02 正则表达式、grep的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/671455

相关文章

PowerShell中15个提升运维效率关键命令实战指南

《PowerShell中15个提升运维效率关键命令实战指南》作为网络安全专业人员的必备技能,PowerShell在系统管理、日志分析、威胁检测和自动化响应方面展现出强大能力,下面我们就来看看15个提升... 目录一、PowerShell在网络安全中的战略价值二、网络安全关键场景命令实战1. 系统安全基线核查

JavaSE正则表达式用法总结大全

《JavaSE正则表达式用法总结大全》正则表达式就是由一些特定的字符组成,代表的是一个规则,:本文主要介绍JavaSE正则表达式用法的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录常用的正则表达式匹配符正则表China编程达式常用的类Pattern类Matcher类PatternSynta

Python中re模块结合正则表达式的实际应用案例

《Python中re模块结合正则表达式的实际应用案例》Python中的re模块是用于处理正则表达式的强大工具,正则表达式是一种用来匹配字符串的模式,它可以在文本中搜索和匹配特定的字符串模式,这篇文章主... 目录前言re模块常用函数一、查看文本中是否包含 A 或 B 字符串二、替换多个关键词为统一格式三、提

python常用的正则表达式及作用

《python常用的正则表达式及作用》正则表达式是处理字符串的强大工具,Python通过re模块提供正则表达式支持,本文给大家介绍python常用的正则表达式及作用详解,感兴趣的朋友跟随小编一起看看吧... 目录python常用正则表达式及作用基本匹配模式常用正则表达式示例常用量词边界匹配分组和捕获常用re

Linux基础命令@grep、wc、管道符的使用详解

《Linux基础命令@grep、wc、管道符的使用详解》:本文主要介绍Linux基础命令@grep、wc、管道符的使用,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐... 目录grep概念语法作用演示一演示二演示三,带选项 -nwc概念语法作用wc,不带选项-c,统计字节数-

正则表达式r前缀使用指南及如何避免常见错误

《正则表达式r前缀使用指南及如何避免常见错误》正则表达式是处理字符串的强大工具,但它常常伴随着转义字符的复杂性,本文将简洁地讲解r的作用、基本原理,以及如何在实际代码中避免常见错误,感兴趣的朋友一... 目录1. 字符串的双重翻译困境2. 为什么需要 r?3. 常见错误和正确用法4. Unicode 转换的

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Python中使用正则表达式精准匹配IP地址的案例

《Python中使用正则表达式精准匹配IP地址的案例》Python的正则表达式(re模块)是完成这个任务的利器,但你知道怎么写才能准确匹配各种合法的IP地址吗,今天我们就来详细探讨这个问题,感兴趣的朋... 目录为什么需要IP正则表达式?IP地址的基本结构基础正则表达式写法精确匹配0-255的数字验证IP地

正则表达式高级应用与性能优化记录

《正则表达式高级应用与性能优化记录》本文介绍了正则表达式的高级应用和性能优化技巧,包括文本拆分、合并、XML/HTML解析、数据分析、以及性能优化方法,通过这些技巧,可以更高效地利用正则表达式进行复杂... 目录第6章:正则表达式的高级应用6.1 模式匹配与文本处理6.1.1 文本拆分6.1.2 文本合并6