作业-第04周--课堂-Day15-正则表达式与三剑客知识应用实

2019-03-23  本文已影响0人  MineG

Day15 课堂笔记

1. 正则表达式

什么是正则表达式?

~~简单地说,正则表达式就是为了处理大量的字符串及文本而定义的一套规则和方法。假设"@"代表"I am","!"代表"oldboy",则执行echo "@!"的结果就是输出"I am oldboy"。通过这些特殊符号的辅助,管理员就可以快速过滤、替换或输出需要的字符串。
~~linux三剑客的正则表达式有以下几个特点:

为什么要学习正则表达式?

~~实际企业中,运维工程师在做linux运维工作时,通常都会面对大量的带有字符串的内容,比如文本配置、程序、命令输出及日志文件等,我们要从大量的字符串内容中查找符合工作需要的特定的字符串。这就需要靠正则表达式了。

有关正则表达式容易混淆的事项!

~~正则表达式的应用非常广泛,在这里,我们所讲的是linux系统运维工作中的正则表达式,就是linux三剑客grep(egrep)sedawk

学习正则表达式注意事项。

1、注意LC_ALL环境变量的设置:

 export LC_ALL=C

2、给grepegrep设置别名(CentOS6需要,CentOS67不需要)

alias grep='grep --color=auto'
alias egrep='egrep --color=auto'

完整的处理及生效命令为:

cat >>/etc/profile<<EOF
alias grep='grep --color=auto'
alias egrep='egrep --color=auto'
export LC_ALL=C
EOF
source /etc/profile

2 基本与扩展正则表达式集合

老男孩linux58期-linux正则表达式

3 基本正则表达式实践

[root@oldboyedu  ~]# mkdir ~/test -p
[root@oldboyedu  ~]# cat >~/test/oldboy.txt<<EOF
> I am oldboy teacher!
> I teach linux.
> 
> I like badminton ball ,billiard ball and chinese chess!
> our site is http://www.oldboyedu.com
> my qq num is 49000448.
> 
> not 4900000448.
> my god ,i am not oldbey,but OLDBOY!
> EOF

最终文件如下:

[root@oldboyedu  ~]# cd ~/test/
[root@oldboyedu  ~/test]# cat -n oldboy.txt 
     1  I am oldboy teacher!
     2  I teach linux.
     3  
     4  I like badminton ball ,billiard ball and chinese chess!
     5  our site is http://www.oldboyedu.com
     6  my qq num is 49000448.
     7  
     8  not 4900000448.
     9  my god ,i am not oldbey,but OLDBOY!

1 ^(尖角号)功能实践

[root@oldboyedu  ~/test]# grep -n "^m" oldboy.txt 
6:my qq num is 49000448.
9:my god ,i am not oldbey,but OLDBOY!

2 $(美元符)功能实践

[root@oldboyedu  ~/test]# grep -n "m$" oldboy.txt 
5:our site is http://www.oldboyedu.com

默认情况下,linux下的所有的文件结尾都有一个$符,查看如下:

root@oldboyedu  ~/test]# grep -n "m$" oldboy.txt | cat -A
5:our site is http://www.oldboyedu.com$

3 ^$功能实践

[root@oldboyedu  ~/test]# grep -n "^$" oldboy.txt 
3:
7:

4 .(点)功能实践

[root@oldboyedu  ~/test]# grep -n "." oldboy.txt 
1:I am oldboy teacher!
2:I teach linux.
4:I like badminton ball ,billiard ball and chinese chess!
5:our site is http://www.oldboyedu.com
6:my qq num is 49000448.
8:not 4900000448.
9:my god ,i am not oldbey,but OLDBOY!

5 \(转义符)功能实践

[root@oldboyedu  ~/test]# grep -n "\.$" oldboy.txt 
2:I teach linux.
6:my qq num is 49000448.
8:not 4900000448.

6 *(星号)功能实践

[root@oldboyedu  ~/test]# grep -n "0*" oldboy.txt 
1:I am oldboy teacher!
2:I teach linux.
3:
4:I like badminton ball ,billiard ball and chinese chess!
5:our site is http://www.oldboyedu.com
6:my qq num is 49000448.
7:
8:not 4900000448.
9:my god ,i am not oldbey,but OLDBOY!

7 .*组合符功能实践

[root@oldboyedu  ~/test]# grep -n ".*" oldboy.txt 
1:I am oldboy teacher!
2:I teach linux.
3:
4:I like badminton ball ,billiard ball and chinese chess!
5:our site is http://www.oldboyedu.com
6:my qq num is 49000448.
7:
8:not 4900000448.
9:my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu  ~/test]# grep -n "^.*o" oldboy.txt 
1:I am oldboy teacher!
4:I like badminton ball ,billiard ball and chinese chess!
5:our site is http://www.oldboyedu.com
8:not 4900000448.
9:my god ,i am not oldbey,but OLDBOY!

8 [](中括号)功能实践

[root@oldboyedu  ~/test]# grep -n '[A-Z]' oldboy.txt 
1:I am oldboy teacher!
2:I teach linux.
4:I like badminton ball ,billiard ball and chinese chess!
9:my god ,i am not oldbey,but OLDBOY!

9 [^abc](中括号内取反符)功能实践

[root@oldboyedu  ~/test]# grep -n '[^a-z]' oldboy.txt 
1:I am oldboy teacher!
2:I teach linux.
4:I like badminton ball ,billiard ball and chinese chess!
5:our site is http://www.oldboyedu.com
6:my qq num is 49000448.
8:not 4900000448.
9:my god ,i am not oldbey,but OLDBOY!```

4 扩展正则表达式实践

1 +(加号)功能实践

[root@oldboyedu  ~/test]# egrep "0+" oldboy.txt
my qq num is 49000448.
not 4900000448.
[root@oldboyedu  ~/test]# egrep -o "0+" oldboy.txt
000
00000

2 ?(问号)功能实践

[root@oldboyedu  ~/test]# cat >oldgril.txt <<EOF
> good
> glad
> gd
> god
> good
> EOF
[root@oldboyedu  ~/test]# egrep 'go?d' oldgril.txt 
gd
god

3 |(竖线)功能实践

[root@oldboyedu  ~/test]# egrep '3306|1521' /etc/services 
mysql           3306/tcp                        # MySQL
mysql           3306/udp                        # MySQL
ncube-lm        1521/tcp                # nCube License Manager
ncube-lm        1521/udp                # nCube License Manager

4 ()(小括号)功能实践

1.) 分组功能实践

要求:取出包含goodglad的行

不用小括号()时:

[root@oldboyedu  ~/test]# cat oldgril.txt 
good
glad
gd
god
good
goood
[root@oldboyedu  ~/test]# egrep 'goo|lad' oldgril.txt 
good
glad
good
goood

多了一个goood,不准确,那么用小括号试试:

[root@oldboyedu  ~/test]# cat oldgril.txt 
good
glad
gd
god
good
goood
[root@oldboyedu  ~/test]# egrep 'g(oo|la)d' oldgril.txt 
good
glad
good

2.) 小括号的后引向功能实践

[root@oldboyedu  ~/test]# cat oldgril.txt
good
glad
gd
god
good
goood
[root@oldboyedu  ~/test]# egrep "(o)\1" oldgril.txt 
good
good
goood

5 {n,m}匹配次数功能实践

[root@oldboyedu  ~/test]# egrep "0{3,5}" oldboy.txt 
my qq num is 49000448.
not 4900000448.
[root@oldboyedu  ~/test]# egrep "0{,5}" oldboy.txt 
I am oldboy teacher!
I teach linux.

I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com
my qq num is 49000448.

not 4900000448.
my god ,i am not oldbey,but OLDBOY!
[root@oldboyedu  ~/test]# egrep "0{3,}" oldboy.txt 
my qq num is 49000448.
not 4900000448.
[root@oldboyedu  ~/test]# egrep "0{3}" oldboy.txt 
my qq num is 49000448.
not 4900000448.

5 sed :流编辑器(Linux三剑客之一)

sed是操作、过滤和转换文本内容的强大工具。
常用功能有对文件实现快速增删改查(增加、删除、修改、查询),
其中查询的功能中最常用的2大功能是过滤(过滤指定字符串)和取行(取出指定行)。
语法:

sed [选项]  [sed内置命令字符]  [文件]

选项:

注意:如果不使用-i选项sed只是修改在内存中的数据,并不会影响磁盘上的文件

sed的内置命令字符说明

5.1 sed命令实践

准备测试文件:

[root@oldboyedu  ~/test]# cat -n oldboy.txt
     1  I am oldboy teacher!
     2  I like badminton ball ,billiard ball and chinese chess!
     3  our site is http://www.oldboyedu.com
     4  my qq num is 49000448.

问题1:输出oldboy.txt的第2-3行内容。

[root@oldboyedu  ~/test]# sed -n '2,3p' oldboy.txt 
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldboyedu.com

问题2:过滤出含有oldboy字符串的行。

 [root@oldboyedu  ~/test]# sed -n '/oldboy/p' oldboy.txt
I am oldboy teacher!
our site is http://www.oldboyedu.com

问题3:删除含有oldboy字符串的行。

[root@oldboyedu  ~/test]# sed '/oldboy/d' oldboy.txt
I like badminton ball ,billiard ball and chinese chess!
my qq num is 49000448.

问题4:将文件中的oldboy字符串全部替换为oldgirl

vim替换:
:%s#oldboy#oldgirl#g
或者
[root@oldboyedu  ~/test]# sed 's#oldboy#oldgirl#g' oldboy.txt
I am oldgirl teacher!
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldgirledu.com
my qq num is 49000448.
修改文件:
sed -i 's#oldboy#oldgirl#g' oldboy.txt

问题5:将文件中的oldboy字符串全部替换为oldgirl,同时将QQ号码49000448改为31333741

sed -e 's#oldboy#oldgirl#g' -e 's#49000448#31333741#g' oldboy.txt 
I am oldgirl teacher!
I like badminton ball ,billiard ball and chinese chess!
our site is http://www.oldgirledu.com
my qq num is 31333741.

问题6:删除指定行,比如第3行或1-4行

sed -i '3d' oldboy.txt  
sed -i '1,4d' oldboy.txt 

问题6:将abc整体(不区分大小写)替换成123

[root@oldboyedu  ~/test]# cat>abc.txt <<EOF
> abc
> ABc
> ABC
> aBc
> aAA
> BcA
> AAA
> EOF
[root@oldboyedu  ~/test]# cat abc.txt 
abc
ABc
ABC
aBc
aAA
BcA
AAA
[root@oldboyedu  ~/test]# sed -i 's/abc/123/ig' abc.txt
[root@oldboyedu  ~/test]# !cat
cat abc.txt 
123
123
123
123
aAA
BcA
AAA

问题7:在oldboy.txt文件的第二行的下一行追加文I teacher linux.

[root@oldboyedu  ~/test]# cat -n oldboy.txt 
     1  I am oldboy teacher!
     2  I like badminton ball ,billiard ball and chinese chess!
     3  our site is http://www.oldboyedu.com
     4  my qq num is 49000448.
[root@oldboyedu  ~/test]# sed -i '2a I teacher linux.' oldboy.txt
[root@oldboyedu  ~/test]# cat -n oldboy.txt 
     1  I am oldboy teacher!
     2  I like badminton ball ,billiard ball and chinese chess!
     3  I teacher linux.
     4  our site is http://www.oldboyedu.com
     5  my qq num is 49000448.

问题8:在oldboy.txt文件的第二行的下一行追加文I teacher linux at 2i.

[root@oldboyedu  ~/test]# cat -n oldboy.txt 
     1  I am oldboy teacher!
     2  I like badminton ball ,billiard ball and chinese chess!
     3  I teacher linux.
     4  our site is http://www.oldboyedu.com
     5  my qq num is 49000448.
[root@oldboyedu  ~/test]# sed -i '2i I teacher linux at 2i.' oldboy.txt
[root@oldboyedu  ~/test]# cat -n oldboy.txt 
     1  I am oldboy teacher!
     2  I teacher linux at 2i.
     3  I like badminton ball ,billiard ball and chinese chess!
     4  I teacher linux.
     5  our site is http://www.oldboyedu.com
     6  my qq num is 49000448.

问题9:取ip案例

ifconfig eth0|sed -n 2p|sed 's#^.*inet ##g'|sed 's#  netm.*$##g'
10.0.0.201
或者
ifconfig eth0|sed -n 2p|sed -e 's#^.*inet ##g' -e 's#  netm.*$##g'
10.0.0.201
或者
ifconfig eth0|sed -ne 's#^.*inet ##g' -e 's#  netm.*$##gp'
10.0.0.201
或者
ifconfig eth0|sed -nr '2s#^.*inet (.*)  netm.*$#\1#gp'
10.0.0.201
或者
ip add|sed -rn 's#^.*net (.*)/24.*$#\1#gp'

6 cut:按列切割

参数:

[root@oldboyedu ~]# cat a.txt 
1 2 3 4 5 6 7 8 9 10
[root@oldboyedu ~]# cut -d" " -f1,3,5 a.txt 
1 3 5
[root@oldboyedu ~]# cut -d" " -f3-5 a.txt 
3 4 5
[root@oldboyedu ~]# sed -n '1,5p' /etc/passwd >oldboyedu.txt
[root@oldboyedu ~]# cat oldboyedu.txt
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin
[root@oldboyedu ~]# cut -d":" -f3,4 /etc/passwd
0:0
1:1
2:2
[root@oldboyedu ~]# cat b.txt 
oldboy 49000448
[root@oldboyedu ~]# cut -c1-6,8- b.txt 
oldboy4900044

7 awk (Linux三剑客之一)

语法:

awk  [option]   'pattern{action}' file ...
awk   [参数]      '条件{动作}'    文件 ...

参数:

变量名 属性
$0 整行
$n 第n列(-F指定分隔符)
NF 记录列的个数,就是有多少列
$NF 最后1列
$(NF-1) 倒数第2列
NR 显示行号

案例:
环境准备:

[root@oldboyedu  ~/test]# sed -n '1,5p' /etc/passwd > test.txt
[root@oldboyedu  ~/test]# cat -n test.txt
     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
     4  adm:x:3:4:adm:/var/adm:/sbin/nologin
     5  lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

问题1:取test.txt文件的第2行到第3行的内容。

[root@oldboyedu ~/test]# awk 'NR>1&&NR<4'  test.txt 
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
或者
[root@oldboyedu  ~/test]# awk 'NR==2,NR==3' test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin

问题2:过滤出含有root字符串的行。

[root@oldboyedu ~/test/]# awk '/root/'  test.txt
root:x:0:0:root:/root:/bin/bash
[root@oldboyedu ~/test/]# awk /root/  test.txt
root:x:0:0:root:/root:/bin/bash
[root@oldboyedu ~/tstst/]# awk "/root/"  test.txt
root:x:0:0:root:/root:/bin/bash

问题3:删除含有root字符串的行。

[root@oldboyedu ~/test/]# awk '/^[^r]/'  test.txt
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

[^r]  非r
^[^r] 以非r字符开头

问题4:取文件的第一列、第三列和最后一列内容,并打印行号。

[root@oldboyedu ~/test/]# awk -F ":" '{print NR,$1,$3,$NF}' test.txt
1 root 0 /bin/bash
2 bin 1 /sbin/nologin
3 daemon 2 /sbin/nologin
4 adm 3 /sbin/nologin
5 lp 4 /sbin/nologin

问题5:取出Linux中执行ifconfig eth0后对应的IP地址(只能输出IP地址)。

[root@oldboyedu ~]# ifconfig eth0|awk 'NR==2{print $2}'
10.0.0.201

问题6:过滤文件中第一列内容匹配root的字符串,把符合的行的最后一列输出

[root@oldboyedu  ~/test]# awk -F ":" '$1~/root/ {print $NF}' test.txt 
/bin/bash
上一篇下一篇

猜你喜欢

热点阅读