#flags 数字 表示新文本替换的模式 g: 表示用新文本替换现有文本的全部实例 p: 表示打印原始的内容 w filename: 将替换的结果写入文件
2.1)sed内部命令说明
演示实例文档
1 2 3 4 5 6
[root@test ~]# cat data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
在data1的每行后追加一行新数据内容: append data "haha" [root@test ~]# sed 'a\append data "haha"' data1 1 the quick brown fox jumps over the lazy dog. append data "haha" 2 the quick brown fox jumps over the lazy dog. append data "haha" 3 the quick brown fox jumps over the lazy dog. append data "haha" 4 the quick brown fox jumps over the lazy dog. append data "haha" 5 the quick brown fox jumps over the lazy dog. append data "haha"
在第二行后新开一行追加数据: append data "haha" [root@test ~]# sed '2a\append data "haha"' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. append data "haha" 3 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
在第二到四行每行后新开一行追加数据: append data "haha" [root@test ~]# sed '2,4a\append data "haha"' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. append data "haha" 3 the quick brown fox jumps over the lazy dog. append data "haha" 4 the quick brown fox jumps over the lazy dog. append data "haha" 5 the quick brown fox jumps over the lazy dog.
匹配字符串追加: 找到包含"3 the"的行,在其后新开一行追加内容: append data "haha" [root@test ~]# sed '/3 the/a\append data "haha"' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy dog. append data "haha" 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
在data1的每行前插入一行新数据内容: insert data "haha" [root@test ~]# sed 'i\insert data "haha"' data1 insert data "haha" 1 the quick brown fox jumps over the lazy dog. insert data "haha" 2 the quick brown fox jumps over the lazy dog. insert data "haha" 3 the quick brown fox jumps over the lazy dog. insert data "haha" 4 the quick brown fox jumps over the lazy dog. insert data "haha" 5 the quick brown fox jumps over the lazy dog.
在第二行前新开一行插入数据: insert data "haha" [root@test ~]# sed '2i\insert data "haha"' data1 1 the quick brown fox jumps over the lazy dog. insert data "haha" 2 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
在第二到四行每行前新开一行插入数据: insert data "haha" [root@test ~]# sed '2,4i\insert data "haha"' data1 1 the quick brown fox jumps over the lazy dog. insert data "haha" 2 the quick brown fox jumps over the lazy dog. insert data "haha" 3 the quick brown fox jumps over the lazy dog. insert data "haha" 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
匹配字符串插入: 找到包含"3 the"的行,在其前新开一行插入内容: insert data "haha" [root@test ~]# sed '/3 the/i\insert data "haha"' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. insert data "haha" 3 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
从标准输出流中做替换,将test替换为text [root@test ~]# echo "this is a test" |sed 's/test/text/' this is a text
将data1中每行的dog替换为cat [root@test ~]# sed 's/dog/cat/' data1 1 the quick brown fox jumps over the lazy cat. 2 the quick brown fox jumps over the lazy cat. 3 the quick brown fox jumps over the lazy cat. 4 the quick brown fox jumps over the lazy cat. 5 the quick brown fox jumps over the lazy cat.
将data1中第二行的dog替换为cat [root@test ~]# sed '2s/dog/cat/' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy cat. 3 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
将data1中第二到第四行的dog替换为cat [root@test ~]# sed '2,4s/dog/cat/' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy cat. 3 the quick brown fox jumps over the lazy cat. 4 the quick brown fox jumps over the lazy cat. 5 the quick brown fox jumps over the lazy dog.
匹配字符串替换:将包含字符串"3 the"的行中的dog替换为cat [root@test ~]# sed '/3 the/s/dog/cat/' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy cat. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
将data1文件中的所有行的内容更改为: change data "data" [root@test ~]# sed 'c\change data "haha"' data1 change data "haha" change data "haha" change data "haha" change data "haha" change data "haha"
将data1文件第二行的内容更改为: change data "haha" [root@test ~]# sed '2c\change data "haha"' data1 1 the quick brown fox jumps over the lazy dog. change data "haha" 3 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
将data1文件中的第二、三、四行的内容更改为:change data "haha" [root@test ~]# sed '2,4c\change data "haha"' data1 1 the quick brown fox jumps over the lazy dog. change data "haha" 5 the quick brown fox jumps over the lazy dog.
将data1文件中包含"3 the"的行内容更改为: change data "haha" [root@test ~]# sed '/3 the/c\change data "data"' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. change data "data" 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
文件内容修改操作–字符转换,将一行中匹配的内容替换为新的数据,使用命令y。
演示案例
1 2 3 4 5 6 7
将data1中的a b c字符转换为对应的 A B C字符 [root@test ~]# sed 'y/abc/ABC/' data1 1 the quiCk Brown fox jumps over the lAzy dog. 2 the quiCk Brown fox jumps over the lAzy dog. 3 the quiCk Brown fox jumps over the lAzy dog. 4 the quiCk Brown fox jumps over the lAzy dog. 5 the quiCk Brown fox jumps over the lAzy dog.
删除文件data1中的第三行数据 [root@test ~]# sed '3d' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
删除文件data1第三到第四行的数据 [root@test ~]# sed '3,4d' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
删除文件data1中包含字符串"3 the"的行 [root@test ~]# sed '/3 the/d' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
打印data1文件内容 [root@test ~]# sed 'p' data1 1 the quick brown fox jumps over the lazy dog. 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
打印data1文件第三行的内容 [root@test ~]# sed '3p' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
打印data1文件第二、三、四行内容 [root@test ~]# sed '2,4p' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
打印data1文件包含字符串"3 the"的行 [root@test ~]# sed '/3 the/p' data1 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
将brown替换为green dog替换为cat [root@test ~]# sed -e 's/brown/green/;s/dog/cat/' data1 1 the quick green fox jumps over the lazy cat. 2 the quick green fox jumps over the lazy cat. 3 the quick green fox jumps over the lazy cat. 4 the quick green fox jumps over the lazy cat. 5 the quick green fox jumps over the lazy cat.
从文件读取编辑器命令 -f 适用于日常重复执行的场景
1 2 3 4 5 6 7 8 9 10 11 12 13
1)将命令写入文件 [root@test ~]# vim abc s/brown/green/ s/dog/cat/ s/fox/elephant/
2)使用-f命令选项调用命令文件 [root@test ~]# sed -f abc data1 1 the quick green elephant jumps over the lazy cat. 2 the quick green elephant jumps over the lazy cat. 3 the quick green elephant jumps over the lazy cat. 4 the quick green elephant jumps over the lazy cat. 5 the quick green elephant jumps over the lazy cat.
抑制内存输出 -n
1 2 3 4 5 6
打印data1文件的第二行到最后一行内容 $最后的意思 [root@test ~]# sed -n '2,$p' data1 2 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
使用正则表达式 -r
1 2 3
打印data1中以字符串"3 the"开头的行内容 [root@test ~]# sed -n -r '/^(3 the)/p' data1 3 the quick brown fox jumps over the lazy dog.
4)打印比较一下,发现data1已经被修改,data1.bak是源文件的备份。 [root@test ~]# cat data1 1 the quick green fox jumps over the lazy dog. 2 the quick green fox jumps over the lazy dog. 3 the quick green fox jumps over the lazy dog. 4 the quick green fox jumps over the lazy dog. 5 the quick green fox jumps over the lazy dog. [root@test ~]# cat data1.bak 1 the quick brown fox jumps over the lazy dog. 2 the quick brown fox jumps over the lazy dog. 3 the quick brown fox jumps over the lazy dog. 4 the quick brown fox jumps over the lazy dog. 5 the quick brown fox jumps over the lazy dog.
2.3)标志
在sed命令中,标志是对sed中的内部命令做补充说明
1 2 3 4 5 6 7
演示文档 [root@test ~]# cat data2 1 the quick brown fox jumps over the lazy dog . dog 2 the quick brown fox jumps over the lazy dog . dog 3 the quick brown fox jumps over the lazy dog . dog 4 the quick brown fox jumps over the lazy dog . dog 5 the quick brown fox jumps over the lazy dog . dog
替换一行中的第二处dog为cat [root@test ~]# sed 's/dog/cat/2' data2 1 the quick brown fox jumps over the lazy dog . cat 2 the quick brown fox jumps over the lazy dog . cat 3 the quick brown fox jumps over the lazy dog . cat 4 the quick brown fox jumps over the lazy dog . cat 5 the quick brown fox jumps over the lazy dog . cat
g标志:将一行中的所有符合的字符串全部执行替换
1 2 3 4 5 6 7
将data1文件中的所有dog替换为cat [root@test ~]# sed 's/dog/cat/g' data2 1 the quick brown fox jumps over the lazy cat . cat 2 the quick brown fox jumps over the lazy cat . cat 3 the quick brown fox jumps over the lazy cat . cat 4 the quick brown fox jumps over the lazy cat . cat 5 the quick brown fox jumps over the lazy cat . cat
p标志:打印文本内容,类似于-p命令选项
1 2 3 4 5 6 7
[root@test ~]# sed '3s/dog/cat/p' data2 1 the quick brown fox jumps over the lazy dog . dog 2 the quick brown fox jumps over the lazy dog . dog 3 the quick brown fox jumps over the lazy cat . dog 3 the quick brown fox jumps over the lazy cat . dog 4 the quick brown fox jumps over the lazy dog . dog 5 the quick brown fox jumps over the lazy dog . dog
w filename标志:将修改的内容存入filename文件中
1 2 3 4 5 6 7 8 9 10
[root@test ~]# sed '3s/dog/cat/w text' data2 1 the quick brown fox jumps over the lazy dog . dog 2 the quick brown fox jumps over the lazy dog . dog 3 the quick brown fox jumps over the lazy cat . dog 4 the quick brown fox jumps over the lazy dog . dog 5 the quick brown fox jumps over the lazy dog . dog
可以看出,将修改的第三行内容存在了text文件中 [root@test ~]# cat text 3 the quick brown fox jumps over the lazy cat . dog
#备份配置文件 conf=/etc/vsftpd/vsftpd.conf \cp $conf $conf.default #根据需求修改配置文件 sed -ir '/^#|^$/d' $conf sed -i '/local_enable/c\local_enable=NO' $conf sed -i '$a anon_upload_enable=YES' $conf sed -i '$a anon_mkdir_write_enable=YES' $conf sed -i '$a anon_other_write_enable=YES' $conf sed -i '$a anon_max_rate=512000' $conf #启动服务 service vsftpd restart &>/dev/null && echo"vsftpd服务启动成功"
#测试验证 chmod 777 /var/ftp/pub cp /etc/hosts /var/ftp/pub #测试下载 cd /tmp lftp $ipaddr <<end cd pub get hosts exit end
if [ -f /tmp/hosts ];then echo "匿名用户下载成功" rm -f /tmp/hosts else echo "匿名用户下载失败" fi #测试上传、创建目录、删除目录等 cd /tmp lftp $ipaddr << end cd pub mkdir test1 mkdir test2 put /etc/group rmdir test2 exit end
if [ -d /var/ftp/pub/test1 ];then echo "创建目录成功" if [ ! -d /var/ftp/pub/test2 ];then echo "文件删除成功" fi else if [ -f /var/ftp/pub/group ];then echo "文件上传成功" else echo "上传、创建目录删除目录部ok" fi fi [ -f /var/ftp/pub/group ] && echo "上传文件成功"