sed
命令
p  打印匹配行

[root@teacher test]# sed '/north/p' datafile
northwest       NW      Charles Main            3.0     .98     3       34
northwest       NW      Charles Main            3.0     .98     3       34
western         WE      Sharon Gray             5.3     .97     5       23
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southern        SO      Suan Chin               5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage               4.4     .84     5       20
northeast       NE      AM Main Jr.             5.1     .94     3       13
northeast       NE      AM Main Jr.             5.1     .94     3       13
western         WE      Sharon Gray             5.3     .97     5       23
north           NO      Margot Weber            4.5     .89     5       9
north           NO      Margot Weber            4.5     .89     5       9
central         CT      Ann Stephens            5.7     .94     5       13

[root@teacher test]# sed -n '/north/p' datafile
northwest       NW      Charles Main            3.0     .98     3       34
northeast       NE      AM Main Jr.             5.1     .94     3       13
north           NO      Margot Weber            4.5     .89     5       9

[root@teacher test]# sed -n '/west/,/east/p' datafile

[root@teacher test]# sed -n '1p' datafile
northwest       NW      Charles Main            3.0     .98     3       34
[root@teacher test]# head -n 1 datafile 
northwest       NW      Charles Main            3.0     .98     3       34

[root@teacher test]# sed -n '$p' datafile
central         CT      Ann Stephens            5.7     .94     5       13

[root@teacher test]# tail -n 1 datafile 
central         CT      Ann Stephens            5.7     .94     5       13

--------------
d  删除匹配行

[root@teacher test]# sed '/north/d' datafile 
western         WE      Sharon Gray             5.3     .97     5       23
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southern        SO      Suan Chin               5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage               4.4     .84     5       20
western         WE      Sharon Gray             5.3     .97     5       23
central         CT      Ann Stephens            5.7     .94     5       13

[root@teacher test]# sed '1d' datafile
western         WE      Sharon Gray             5.3     .97     5       23
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southern        SO      Suan Chin               5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage               4.4     .84     5       20
northeast       NE      AM Main Jr.             5.1     .94     3       13
western         WE      Sharon Gray             5.3     .97     5       23
north           NO      Margot Weber            4.5     .89     5       9
central         CT      Ann Stephens            5.7     .94     5       13
-------------------------
s  查找替换
    /定址/s/匹配模式/替换内容/标记

匹配模式: 正则表达式
标记:  num 替换第几次出现的模式
       p 打印替换内容
       w 另存
       g 全局替换

[root@teacher test]# sed 's/root/ROOT/g' passwd

[root@teacher test]# sed -n 's/root/ROOT/gp' passwd
ROOT:x:0:0:ROOT:/ROOT:/bin/bash
roooot:x:0:0:ROOT:/ROOT:/bin/bash
ROOTooo:x:0:0:/bin/bash:arooootbc
ro:x:0:0:ROOT:/ROOT:/bin/bash
operator:x:11:0:operator:/ROOT:/sbin/nologin

[root@teacher test]# sed -n '1s/root/ROOT/gp' passwd
ROOT:x:0:0:ROOT:/ROOT:/bin/bash

[root@teacher test]# sed -n '/roooot:/s/root/ROOT/gp' passwd
roooot:x:0:0:ROOT:/ROOT:/bin/bash
[root@teacher test]# sed -n '/r\(o\)\1\1\1t:/s/root/ROOT/gp' passwd
roooot:x:0:0:ROOT:/ROOT:/bin/bash

[root@teacher test]# sed -n '/NW/s/west/north/p' datafile

[root@teacher test]# sed -n 's/root/**&**/p' passwd
**root**:x:0:0:root:/root:/bin/bash
roooot:x:0:0:**root**:/root:/bin/bash

[root@teacher test]# sed -n 's/root/hello&/gp' passwd

[root@teacher test]# sed -n 's/root/hello&/gp' passwd
helloroot:x:0:0:helloroot:/helloroot:/bin/bash
roooot:x:0:0:helloroot:/helloroot:/bin/bash

[root@teacher test]# sed -n 's/root/ROOT/2p' passwd
root:x:0:0:ROOT:/root:/bin/bash
roooot:x:0:0:root:/ROOT:/bin/bash

[root@teacher test]# sed -n 's/root/ROOT/w  /test/file2' passwd 
You have new mail in /var/spool/mail/root
[root@teacher test]# cat /test/file2 
ROOT:x:0:0:root:/root:/bin/bash
roooot:x:0:0:ROOT:/root:/bin/bash

练习: 把/etc/vsftpd/vsftpd.conf 里面的 #anon_upload_enable=YES 注释去掉

方法1 # sed -n 's/#anon_upload_enable=YES/anon_upload_enable=YES/p' /etc/vsftpd/vsftpd.conf
anon_upload_enable=YES

方法2 [root@teacher test]# sed -n '/#anon_upload_enable=YES/s/^#//p' /etc/vsftpd/vsftpd.conf
anon_upload_enable=YES


方法3 [root@teacher test]# sed -n 's/#\(anon_upload_enable=YES\)/\1/p' /etc/vsftpd/vsftpd.conf
anon_upload_enable=YES

方法4 [root@teacher test]# sed -n 's/\(#\)\(anon_upload_enable=YES\)/\2/p' /etc/vsftpd/vsftpd.conf
anon_upload_enable=YES

[root@teacher test]# 
-------------------------
r  读入文件的内容

[root@teacher test]# cat a1
hello
hello2

# cat a2
tom
mike

[root@teacher test]# sed 'r a2'  a1
hello
tom
mike
hello2
tom
mike

-------------------
w  另存为

# sed '1,3w  file2' passwd

-----------------
a  在匹配行的下面添加内容

[root@teacher test]# sed '1a hello all' passwd
root:x:0:0:root:/root:/bin/bash
hello all

# sed -n '/#anon_upload_enable=YES/a  anon_upload_enable=YES' /etc/vsftpd/vsftpd.conf

# sed '/^north/a the north has moved'  datafile

--------------------
i  在匹配行的上面添加内容

[root@teacher test]# sed '1i hello all' passwd 
--------------------
c  本行替换

# sed '1c  dajihao' passwd|less
# sed '/^north/c  he has gone' datafile
---------------------
y 按照位置替换

# sed 'y/abcdefghijklmnopqrstuvwxyz/zyxwvutsrqponmlkjihgfedcba/'  s1

---------------------
;;;    多点编辑
-e '' -e ''

# sed -e 'p' -e 'p' s1

# sed 'p;p;p' s1
练习: 把/etc/ssh/sshd_config 87行注释去掉,88行加上注释
# vim /etc/ssh/sshd_config
 87 #UsePAM no
 88 UsePAM yes

[root@teacher test]# sed -e 's/#\(UsePAM no\)/\1/' -e 's/\(UsePAM yes\)/#\1/' /etc/ssh/sshd_config

--------------------------
-r  扩展正则

# sed 's/^\(.\)\(.\)/\1/' s1
# sed -r 's/^(.)(.)/\1/' s1

# cat -n passwd | sed -n '5~5p'
     5  bin:x:1:1:bin:/bin:/sbin/nologin
    10  shutdown:x:6:0:shutdown:/sbin:/sbin/shutdown
    15  operator:x:11:0:operator:/root:/sbin/nologin
    20  nscd:x:28:28:NSCD Daemon:/:/sbin/nologin
    25  pcap:x:77:77::/var/arpwatch:/sbin/nologin
    30  rpcuser:x:29:29:RPC Service User:/var/lib/nfs:/sbin/nologin
    35  gdm:x:42:42::/var/gdm:/sbin/nologin
    40  tomson:x:503:500::/home/tomson:/bin/bash
    45  u3:x:1001:1001::/home/u3:/bin/bash
    50  tom:x:1006:1007::/home/tom:/bin/bash
    55  s1:x:1008:1009::/rhome/s1:/bin/bash
    60  s6:x:1013:1014::/rhome/s6:/bin/bash
---------------------------
n  处理匹配的下一行

# vim a3
1
2
3
4
5
6
7
8

[root@teacher test]# sed '3n; s/[0-9]/A/' a3
A
A
3
A
A
A
A
A

[root@teacher test]# sed '5n; s/[0-9]/A/' a3
A
A
A
A
5
A
A
A

[root@teacher test]# sed '2n;5n; s/[0-9]/A/' a3
A
2
A
A
5
A
A
A

[root@teacher test]# sed 's/[0-9]/A/;2n;5n;s/A/B/' a3
B
A
3
B
A
6
B
B

[root@teacher test]# sed 's/[0-9]/A/;2n;5n;s/[0-9]/B/' a3
A
A
B
A
A
B
A
A

------------------------------------
模式空间:PATT,处理文件的内容,对输入行使用命令进行处理
保留空间:HOLD,默认有一个空行,保存已经处理过的输入行的空间,也在内存上。

h:将模式空间的内容,复制到保留空间,覆盖原来的内容
H:将模式空间的内容,追加到保留空间,原内容保留
g:将保留空间的内容,复制到模式空间,覆盖原来的内容
G:将保留空间的内容,追加到模式空间,原内容保留
x:交换模式空间和保留空间的内容

# vim a5
this is a good idea.
no, it is'nt.
good day.

# sed -e '1h' -e '$G' a5
this is a good idea.
no, it is'nt.
good day.
this is a good idea.

# sed -e '1h' -e '$g' a5
this is a good idea.
no, it is'nt.
this is a good idea.

# sed -e '1h;1d' -e '$G' a5
no, it is'nt.
good day.
this is a good idea.

[root@teacher test]# sed -e '/northeast/H' -e '$G' datafile
northwest       NW      Charles Main            3.0     .98     3       34
western         WE      Sharon Gray             5.3     .97     5       23
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southern        SO      Suan Chin               5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage               4.4     .84     5       20
northeast       NE      AM Main Jr.             5.1     .94     3       13
western         WE      Sharon Gray             5.3     .97     5       23
north           NO      Margot Weber            4.5     .89     5       9
central         CT      Ann Stephens            5.7     .94     5       13

northeast       NE      AM Main Jr.             5.1     .94     3       13
You have new mail in /var/spool/mail/root
[root@teacher test]# sed -e '/^north/H' -e '$G' datafile
northwest       NW      Charles Main            3.0     .98     3       34
western         WE      Sharon Gray             5.3     .97     5       23
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southern        SO      Suan Chin               5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage               4.4     .84     5       20
northeast       NE      AM Main Jr.             5.1     .94     3       13
western         WE      Sharon Gray             5.3     .97     5       23
north           NO      Margot Weber            4.5     .89     5       9
central         CT      Ann Stephens            5.7     .94     5       13

northwest       NW      Charles Main            3.0     .98     3       34
northeast       NE      AM Main Jr.             5.1     .94     3       13
north           NO      Margot Weber            4.5     .89     5       9
[root@teacher test]# sed -e '/^north/h' -e '$G' datafile
northwest       NW      Charles Main            3.0     .98     3       34
western         WE      Sharon Gray             5.3     .97     5       23
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southern        SO      Suan Chin               5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage               4.4     .84     5       20
northeast       NE      AM Main Jr.             5.1     .94     3       13
western         WE      Sharon Gray             5.3     .97     5       23
north           NO      Margot Weber            4.5     .89     5       9
central         CT      Ann Stephens            5.7     .94     5       13
north           NO      Margot Weber            4.5     .89     5  


[root@teacher test]# sed -e '/WE/{h;d;}'  -e  '/CT/G' datafile
northwest       NW      Charles Main            3.0     .98     3       34
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southern        SO      Suan Chin               5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage               4.4     .84     5       20
northeast       NE      AM Main Jr.             5.1     .94     3       13
north           NO      Margot Weber            4.5     .89     5       9
central         CT      Ann Stephens            5.7     .94     5       13
western         WE      Sharon Gray             5.3     .97     5 


[root@teacher test]# sed -e '/WE/{h;d;}'  -e  '/CT/g' datafile
northwest       NW      Charles Main            3.0     .98     3       34
southwest       SW      Lewis Dalsass           2.7     .8      2       18
southern        SO      Suan Chin               5.1     .95     4       15
southeast       SE      Patricia Hemenway       4.0     .7      4       17
eastern         EA      TB Savage               4.4     .84     5       20
northeast       NE      AM Main Jr.             5.1     .94     3       13
north           NO      Margot Weber            4.5     .89     5       9
western         WE      Sharon Gray             5.3     .97     5 


# sed -e '1h;2,3H' -e  '5G'  s3

----------------------------------
sedsed
# cd /test

# ./sedsed
ERROR: there's no SED script to parse! (try --help)
[root@teacher test]# ./sedsed --help

[root@teacher test]# cat a2
tom
mike
[root@teacher test]# ./sedsed -d '1h;2H;'  a2

----------------------------------------
-i  修改源文件

[root@teacher test]# cp /etc/vsftpd/vsftpd.conf  ./

练习:
    把匿名用户禁止, 系统用户上传的文件权限为666,设置白名单。

--------------------
-f  调用sed脚本

 12 #anonymous_enable=YES

 22 local_umask=022

chroot_local_user is YES,
 96 #chroot_list_enable=YES
 97 # (default follows)
 98 #chroot_list_file=/etc/vsftpd/chroot_list

#  vim vsftp.s

s/#anonymous_enable=YES/anonymous_enable=NO/
s/local_umask=022/local_umask=000/
/chroot_local_user/a  chroot_local_user=YES
s/#\(chroot_list_enable=YES\)/\1/
s/#chroot_list_file/chroot_list_file/

# sed -i -f vsftp.s  vsftpd.conf 

 12 anonymous_enable=NO
 22 local_umask=000
 95 chroot_local_user=YES
 97 chroot_list_enable=YES
 99 chroot_list_file=/etc/vsftpd/chroot_list