今天内容

复习:

-i : 忽略大小写
-n : 显示过滤出来的文本在文件内的行号
-o : 仅显示过滤出来的文本
-q : 静默输出
-v : 反向输出
-E :使用扩展正则表达式
-l : 显示文件路径
-R :递归查询文件内内容
-c : 显示匹配出来的个数

-A : 显示文件之后的两行
-B :显示文件之前的两行
-C :显示文件之前后的两行

2、正则表达式

1、普通正则

* :匹配零个或多个前导字符
. : 匹配一个任意字符
.* : 匹配零个或多个任意字符
[] : 或者
^ : 以某某开头
$ : 以某某结尾
[^] : 取反
\ : 转义字符

2、扩展正则

+ : 匹配至少一个前导字符
? : 匹配零个或一个字符
() : 分组
{} :范围
{m,n} : m 到 n
{m} : m个
{m,} :至少m个
| :或者

练习1:将/etc/fstab文件中所有的注释行和空行过滤掉
[root@localhost tmp]# egrep -v "^$|^#" /etc/fstab
[root@localhost tmp]# egrep "^[^#]" /etc/fstab
[root@localhost ~]# grep -vE "^ *#|^$" /etc/fstab

 

linux三剑客之sed

sed(流式编辑器) : sed主要用来修改文件。

1、sed命令

sed [参数] "[定位][指令]" 处理的文本路径

注:不指定定位,则默认处理全文。

指令:

p : 打印文本
d : 删除文本

参数:

-e : 允许多项编辑
-n : 取消默认输出
-i : 就地编辑文本
-r : 支持扩展正则表达式(sed中的正则表达式必须放在两个//中间)
-f :指定定位规则的文件

案例:

1、在文本中,打印第一行和第五行

[root@localhost ~]# sed -e '5p' -e "1p" 1.txt

2、在文本中,要求只打印第1,5,6三行

[root@localhost ~]# sed -n -e '5p' -e "1p" -e "6p" 1.txt

3、要求删除文件的第3行

[root@localhost ~]# sed -i '3d' 1.txt

4、删除/etc/fstab文件中所有的注释的行

[root@localhost ~]# sed -i -r "/^ *#|^$/d" /etc/fstab

5、删除1.txt的3行,打印第4行

[root@localhost ~]# sed -f 3.txt 1.txt
[root@localhost ~]# cat 3.txt
3d
4p

练习

练习1:要求将/etc/passwd文件复制到/root/1.txt中,然后删除当中包含/sbin/nologin的行

[root@localhost ~]# sed -i -r "/\/sbin\/nologin/d" 4.txt
[root@localhost ~]# cat 4.txt
root:x:0:0:root:/root:/bin/bash
sync:x:5:0:sync:/sbin:/bin/sync
shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
halt:x:7:0:halt:/sbin:/sbin/halt
test02:x:1001:1001::/home/test02:/bin/bash
test03:x:1002:1002::/home/test03:/bin/bash

练习2:将/etc/nginx/nginx.conf文件中所有的注释的行(以#开头的行)全部删除

[root@localhost ~]# sed -i -r "/^ *#/d" /etc/nginx/nginx.conf

2、sed + 正则表达式(定位)

1、数字

1、固定定位
[root@localhost ~]# sed -n '2p' 1.txt

2、范围定位
[root@localhost ~]# sed -n '1,3p' 1.txt

2、正则

正则表达式必须放在/ / 之间

1、打印/etc/passwd文件中包含root的行

[root@localhost ~]# sed -n "/root/p" /etc/passwd
root:x:0:0:root:/root:/bin/bash
operator:x:11:0:operator:/root:/sbin/nologin

2、将包含空格的行打印出来

[root@localhost ~]# sed -n -r "/ +/p" /etc/passwd

3、数字加正则

正则匹配是非贪婪性的匹配

贪婪性是匹配到了之后,不停继续匹配,直至文件所有的内容全部匹配完毕

非贪婪性匹配,一旦匹配到了就停止匹配

1、在/etc/passwd文件中的第一行,到包含test的行,全部删除

[root@localhost ~]# sed -r "1,/test/d" /etc/passwd

2、删除从包含root的行到第5行的内容

[root@localhost ~]# sed -r "/root/,5d" /etc/passwd

3、从包含root的行删除到包含ftp的行

[root@localhost ~]# sed -r "/root/,/ftp/d" /etc/passwd

4、\c与c分隔符

\c与c只是一个代表,其中c可以换成任意一个字符

[root@localhost ~]# sed -i -r "\#/sbin/nologin#d" 4.txt
[root@localhost ~]# sed -i -r "\A/sbin/nologinAd" 4.txt

3、sed的常用指令

p : 打印
d : 删除

a : 在当前行后添加一行或多行内容

[root@localhost ~]# sed "2a xxxxxx" 4.txt

c : 用新文件修改(替换)当前行中的文本

[root@localhost ~]# sed "3c fbsdfbsdbfdsbfdsbfdsfbdsfbdsjfbsdu" 4.txt

i : 在当前行之前插入文本

[root@localhost ~]# sed "3i fbsdfbsdbfdsbfdsbfdsfbdsfbdsjfbsdu" 4.txt

练习1 : 在/etc/passwd文件中1到3行之前插入HelloWorld

[root@localhost ~]# sed "1,3i HelloWorld" /etc/passwd

r :从以外文件中读相关内容,写到相关行之后

[root@localhost ~]# sed "1,3r 5.txt" /etc/passwd

w : 匹配到的行写入一个新的文件之中

[root@localhost ~]# sed "1,5w 6.txt" /etc/passwd

y :将字符转换成一个新的字符

[root@localhost ~]# sed "1,5y/sbin/1234/" /etc/passwd

sbin ---> 1234

s ---> 1
b ---> 2
i ---> 3
n ---> 4

s : 用一个字符替整体替换成另外一个字符

[root@localhost ~]# sed "s/sbin/1234/" /etc/passwd

s指令替换对于行来说,是非贪婪性,如果需要全局替换则需要使用 g 指令

g : 全局执行

[root@localhost ~]# sed "s/root/1234/g" /etc/passwd

i : 配合s指令配合一起使用时,则是忽略大小的作用

[root@localhost ~]# sed "s/ROOT/1234/i" /etc/passwd

练习:

练习1:替换/etc/passwd中的root为ROOT

[root@localhost ~]# sed "s/root/ROOT/g" /etc/passwd

练习2:将模板机(192.168.15.200)中的ip替换成192.168.15.50

[root@localhost ~]# sed "s/\.200/\.50/g" /etc/sysconfig/network-scripts/ifcfg-eth[01]

练习3:删除/etc/passwd中的所有偶数行

[root@localhost ~]# sed "1~2d" /etc/passwd

练习4:在每一行的行首增加#号

[root@localhost ~]# sed "s/^ */#/g" 5.txt

练习5:将Hello World替换成World Hello

[root@localhost ~]# sed -r "s/(Hello) (World)/\2 \1/g" 6.txt

练习6:将1.txt中的每一行都添加一个.bak的后缀

[root@localhost ~]# sed -r "s/(.*)/\1\.bak/g" 1.txt

posted on 2021-09-30 15:42  李辉111  阅读(61)  评论(0编辑  收藏  举报