一、简述
sed行编辑器
stream editor
用来操作纯 ASCII 码的文本
处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),可以指定仅仅处理哪些行
sed处理完成之后把缓冲区的内容送往屏幕,接着处理下一行,这样不断重复, 直到文件末尾
sed符合条件的处理,不符合条件的不予处理,文件内容并没有 改变,除非用重定向存储输出
二、调用 sed 命令有两种形式:
sed [options] 'command' file(s)
sed [options] -f scriptfile file(s)
三,多种操作模式
p 显示 d 删除 a 添加 c 替换 w 写入 i 插入
1,p 模式操作
-n 只列出结果sed特殊处理的那一行,不显示原来那一行
-e 多重编辑
sed -n '/^#/p' fstab 表示显示以#开头的行
sed -n '/^#/!p' fstab 表示以#开头的行不显示
sed -n '/0$/!p' fstab 表示以0结尾的行不显示
sed -n '/0$/p' fstab 表示显示以0结尾结尾的行
[root@desktop mnt]# cat -n fstab
1
2 #
3 # /etc/fstab
4 # Created by anaconda on Wed May 7 01:22:57 2014
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
10 /dev/vg0/vo /home ext4 defaults 0 0
[root@desktop mnt]# cat -n fstab | sed -n '4,7p'
4 # Created by anaconda on Wed May 7 01:22:57 2014
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
显示第二行和第六行
[root@desktop mnt]# cat -n fstab | sed -n '2p;6p'
2 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
sed -n -e '2!p' fstab 表示第二行不显示
sed -n -e '2,5!p' fstab 表示第2到5行不显示
[root@desktop mnt]# cat -n fstab | sed -n -e '2!p'
1
3 # /etc/fstab
4 # Created by anaconda on Wed May 7 01:22:57 2014
5 #
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
10 /dev/vg0/vo /home ext4 defaults 0 0
[root@desktop mnt]# cat -n fstab | sed -n -e '2,5!p'
1
6 # Accessible filesystems, by reference, are maintained under '/dev/disk'
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
10 /dev/vg0/vo /home ext4 defaults 0 0
练习
给定userfile和passfile,批量创建用户
[root@desktop mnt]# vim userfile
[root@desktop mnt]# cat userfile
user1
user2
user3
[root@desktop mnt]# vim passwdfile
[root@desktop mnt]# cat passwdfile
123
123
123
[root@desktop mnt]# vim user.sh
[root@desktop mnt]# cat user
cat: user: No such file or directory
[root@desktop mnt]# cat user.sh
#!/bin/bas
NUM=$( echo `wc -l userfile` | cut -d " " -f 1 )
for i in `seq 1 $NUM `
do
USERNAME=`sed -n "${i}p" userfile`
PASSWORD=`sed -n "${i}p" passwdfile`
useradd $USERNAME
echo $PASSWORD | passwd --stdin $USERNAME
done
[root@desktop mnt]# sh user
userfile user.sh
[root@desktop mnt]# sh user.sh
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
Changing password for user user3.
passwd: all authentication tokens updated successfully.
sed -e '2d;6d' fstab 表示第2行,第6行删除
sed -e '2,6d' fstab 表示第2到6行删除
[root@desktop mnt]# cat -n fstab | sed -e '2d;6d'
1
3 # /etc/fstab
4 # Created by anaconda on Wed May 7 01:22:57 2014
5 #
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
10 /dev/vg0/vo /home ext4 defaults 0 0
[root@desktop mnt]# cat -n fstab | sed -e '2,6d'
1
7 # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
8 #
9 UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
10 /dev/vg0/vo /home ext4 defaults 0 0
ed -e '/^#/d' fstab 表示删除以#开头的行
sed -e '/^$/d;/^#/d' fstab 表示删除空行并且以#开头的行 ^$表示首尾相连,也就是空行
sed -e '/UUID/d' fstab 表示删除含有UUID的行
sed -e '/UUID/!d' fstab 表示不删除含有UUID的行,也就是只显示含有UUID的行
sed '/hello/aword' westos 表示把word加在含有hello后一行 默认是在后面一行追加
sed 's/hello/hello world/g' westos 表示把含有hello的行替换为hello world ,这里g是全文替换,注意前面有s
sed '/hello/aworld westos' westos 表示在含有hello后面一行追加world westos,其中world westos在一行
sed '/hello/aworld\nwestos' westos 表示在含有hello 后面一行追加 world westos,但是有\n表示换行
[root@desktop mnt]# vim westos
[root@desktop mnt]# cat westos
hello
[root@desktop mnt]# sed '/hello/aword' westos
hello
word
[root@desktop mnt]# sed 's/hello/hello world/g' westos
hello world
[root@desktop mnt]# sed '/hello/aworld wesotos' westos
hello
world wesotos
sed '/hello/iworld\nwestos' westos 表示在含有hello行的前面插入world westos,\n表示换行
[root@desktop mnt]# sed '/hello/iworld\nwestos' westos
world
westos
hello
sed '/hello/chello world' westos 表示把含有hello的行替换为hello
sed '/hello/cwestos\nworld' westos 表示把含有hello的行替换为westos world,\n表示换行[root@desktop mnt]# sed '/hello/chello world' westos
hello world
[root@desktop mnt]# sed '/hello/cwestos\nworld' westos
westos
world
sed -n '/bash$/p' passwd > file 重定向写入文件
sed -n '/bash$/wfile' passwd 在w模式下,可以直接写入文件,这是追加的过程,不会覆盖原文
[root@desktop mnt]# touch file
[root@desktop mnt]# sed -n '/bash$/p' passwd > file
sed: can't read passwd: No such file or directory
[root@desktop mnt]# cp /etc/passwd /mnt/
[root@desktop mnt]# sed -n '/bash$/p' passwd > file
[root@desktop mnt]# cat file
root:x:0:0:root:/root:/bin/bash
student:x:1000:1000:Student User:/home/student:/bin/bash
westos:x:1001:1001::/home/westos:/bin/bash
test:x:1002:1002::/home/test:/bin/bash
user1:x:1003:1003::/home/user1:/bin/bash
user2:x:1004:1004::/home/user2:/bin/bash
user3:x:1005:1005::/home/user3:/bin/bash
[root@desktop mnt]#