Shell正则表达式(二)

sed工具概述

sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后,把缓冲区的内容送往屏幕。接着处理下一行,这样不断重复,直到文件末尾。文件内容并没有改变,除非你使用重定向存储输出。Sed主要用来自动编辑一个或多个文件;简化对文件的反复操作;编写转换程序等。

1.工作流程
读取—执行—显示
常见用法

sed   [选项]  '操作'  参数
sed   [选项]  -f  scriptfile  参数

1).常见选项

  • -e <script>或--expression=<script>:以选项中的指定的script来处理输入的文本文件;
  • -f<script文件>或--file=<script文件>:以选项中指定的script文件来处理输入的文本文件;
  • -h或--help:显示帮助;
  • -n或--quiet或——silent:仅显示script处理后的结果;
  • -V或--version:显示版本信息;
  • -i 直接编辑文本文件;
    2).常见操作
  • a: 在当前行下面插入文件;
  • i: 在当前行上面插入文本;
  • c: 把选定的行改为新的文本;
  • d: 删除,删除选择的行;
  • s: 替换指定字符;
  • p: 打印模板块的行;
  • g: 表示行内全面替换;
  • w:表示把行写入一个文件;
  • G:追加到当前模板块文本的后面;
  • =:打印当前行号码;
    2.sed用法示例
    创建测试文件
[root@CentOS-3 ~]# vim test.txt
he was short and fat.
He was wearing a blue polo shirt with black pants.
The home of Football on BBC Sport online.
the tongue is boneless but it breaks bones.12!
google is the best tools for search keyword.
The year ahead will test our political establishment to the li
PI=3.141592653589793238462643383249901429
a wood cross!
Actions speak louder than words

#woood #
#woooooood #
AxyzxyzxyzxyzC
I bet this place is really spooky late at night!
Misfortunes never come alone/single.
I shouldn't have lett so tast.

输入符号条件的文本(p)

输入所有内容,等同于cat test.txt

[root@CentOS-3 ~]# sed -n 'p' test.txt

在这里插入图片描述
输入第3行

[root@CentOS-3 ~]# sed -n '3p' test.txt

在这里插入图片描述
输出第3-5行

[root@CentOS-3 ~]# sed -n '3p' test.txt

在这里插入图片描述
输出所有奇数行(n—表示读入下一行)

[root@CentOS-3 ~]# sed -n 'p;n' test.txt

在这里插入图片描述
输出所有偶数行

[root@CentOS-3 ~]# sed -n 'n;p' test.txt

在这里插入图片描述
输出1-5奇数行

[root@CentOS-3 ~]# sed -n '1,5{p;n}' test.txt

在这里插入图片描述
输出第10行至文件尾的偶数行(其实输出的是10,11,13,15……等行)

[root@CentOS-3 ~]# sed -n '10,${n;p}' test.txt

在这里插入图片描述
输出包含the的行

[root@CentOS-3 ~]# sed -n '/the/p' test.txt

在这里插入图片描述
输出第4行至第1个包含the的行

[root@CentOS-3 ~]# sed -n '4,/the/p' test.txt

在这里插入图片描述
输出包含the的所在行的行号(= 用来输出行号)

[root@CentOS-3 ~]# sed -n '/the/=' test.txt

在这里插入图片描述
输出以PI开头的行

[root@CentOS-3 ~]# sed -n '/^PI/p' test.txt

在这里插入图片描述
输出以数字结尾的行

[root@CentOS-3 ~]# sed -n '/[0-9]$/p' test.txt

在这里插入图片描述
输出包含单词wood的行 < ,>表示单词边界

[root@CentOS-3 ~]# sed -n '/\<wood\>/p' test.txt 

在这里插入图片描述

删除符合条件的文件(d)

nl—计算文件的行数
删除第3行

[root@CentOS-3 ~]# nl test.txt | sed '3d'

在这里插入图片描述
删除3-5行

[root@CentOS-3 ~]# nl test.txt | sed '3,5d'

在这里插入图片描述
删除包含cross的行

[root@CentOS-3 ~]# nl test.txt | sed '/cross/d'

在这里插入图片描述
删除不包含cross的行

[root@CentOS-3 ~]# nl test.txt | sed '/cross/!d'

在这里插入图片描述
删除开头为小写字母的行

[root@CentOS-3 ~]# sed '/^[a-z]/d' test.txt

在这里插入图片描述
删除以‘.'结尾的行

[root@CentOS-3 ~]# sed '/\.$/d' test.txt

在这里插入图片描述
删除空行

[root@CentOS-3 ~]# sed '/^$/d' test.txt

在这里插入图片描述
删除重复的空行

[root@CentOS-3 ~]# sed '/^$/{n;/^$/d}' test.txt
等同于
[root@CentOS-3 ~]# cat -s test.txt

在这里插入图片描述

替换符合条件的文本(s)

将每行中的第1个the替换为THE

[root@CentOS-3 ~]# sed 's/the/THE/' test.txt

在这里插入图片描述
将每行中的第2个l替换为L

[root@CentOS-3 ~]# sed 's/l/L/2' test.txt

在这里插入图片描述
将文件中所有的the替换为THE

[root@CentOS-3 ~]# sed 's/the/THE/g' test.txt

在这里插入图片描述
将文件中所有的o删除(替换为空串)

[root@CentOS-3 ~]# sed 's/o//g' test.txt

在这里插入图片描述
每行开始添加#字符

[root@CentOS-3 ~]# sed 's/^/#/' test.txt

在这里插入图片描述
在包含the的每行行首添加#字符

[root@CentOS-3 ~]# sed '/the/s/^/#/' test.txt

在这里插入图片描述
在每行末尾添加EOF字符

[root@CentOS-3 ~]# sed 's/$/EOF/' test.txt

在这里插入图片描述
将3-5行所有的the替换为THE

[root@CentOS-3 ~]# sed '3,5s/the/THE/g' test.txt

在这里插入图片描述
将包含the的行中的o替换为O

[root@CentOS-3 ~]# sed '/the/s/o/O/g' test.txt

在这里插入图片描述

迁移符合条件的文本
  • H:复制到剪贴板;
  • g,G:将剪贴板中的数据覆盖\追加到指定行;
  • w:保存为文件;
  • r:读取指定文件;
  • a:追加指定内容;

将包含the的行迁移到行尾(;用于多个操作)
H复制到剪贴板---d删除---$G追加到行尾

[root@CentOS-3 ~]# sed '/the/{H;d};$G' test.txt

在这里插入图片描述
将1-5行迁移到17行后

[root@CentOS-3 ~]# sed '1,5{H;d};17G' test.txt

在这里插入图片描述
将包含the的行另存为新文件

[root@CentOS-3 ~]# sed '/the/w out.file' test.txt
[root@CentOS-3 ~]# ls
anaconda-ks.cfg  out.file  test.txt
[root@CentOS-3 ~]# cat out.file

在这里插入图片描述
在包含the每行后添加文件hostname内容

[root@CentOS-3 ~]# sed '/the/r /etc/hostname' test.txt

在这里插入图片描述
在第3行后插入新行,内容为New

[root@CentOS-3 ~]# sed '3aNew' test.txt

在这里插入图片描述
在包含the的每行后插入新行

[root@CentOS-3 ~]# sed '/the/aNew' test.txt

在这里插入图片描述
在第3行后插入多行(\n 换行符)

[root@CentOS-3 ~]# sed '3aNew1\nNew2' test.txt

在这里插入图片描述
5).使用脚本编辑文件
使用sed脚本,将多个编辑指令存放到文件中(每行一条编辑指令),通过 -f 调用
例:将1-5行迁移到17行后

可以使用下列脚本实现
[root@CentOS-3 ~]# vim opt.list
1,5H
1,5d
17G
[root@CentOS-3 ~]# sed -f opt.list test.txt

在这里插入图片描述

使用sed直接操作文件

准备工作:安装vsftpd服务
在这里插入图片描述
编辑脚本:调整vsftpd服务配置:禁止匿名用户,但允许本地用户(也允许写入)

[root@CentOS-3 ~]# vim local_only_ftp.sh
#!/bin/bash
# 指定配置文件样本路径、配置文件路径
sample="/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf"
config="/etc/vsftpd/vsftpd.conf"
#备份原来的配置文件:检测备份文件是否存在,不存在进行备份
[ ! -e "${config}.bak" ] && cp $config ${
    
    config}.bak
# 将样例文件中的以anonymous_enable开头的行中yes替换为no,并覆盖ftp配置文件
sed -e '/^anonymous_enable/s/YES/NO/g' $sample > $config
#查看是否有包含listen的行,否则在文件末尾添加此行
grep "listen" $config || sed -i '$alisten=YES' $config
# 重启服务并开机自启动;查看服务端口号
systemctl restart vsftpd
systemctl enable vsftpd
nsten的行,否则在文件末尾添加此行

在这里插入图片描述运行脚本进行查看
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_46902396/article/details/108608725