linux——sed行编辑器

Sed 行编辑器

Stream editor
       用来操作纯SCLL码的文本,处理时,把当前的行储存在临时缓存区中,称为“模式空间(pattern space)”可以指定仅仅处理哪些行,sed符合模式条件的处理,不符合条件的不处理,处理完成后把缓存区的内容送往屏幕,接着处理下一行,这样不断重复,直到文件末尾。

1.sed 命令格式:

调用sed命令有两种形式:

1.sed [ options ] ‘command’ files(s)       //直接加命令
2. sed [ options ] -f scriptfile files(s)      //使用命令文件

2.Sed 对字符的处理:

p 显示模式
d 删除模式
a 添加模式
c 替换模式
w 写入模式
i 插入模式

2.1 、P(显示)模式

sed  -n  ‘/^#/p’  fstab            //显示以“#”开头的行
sed   -n  ‘/UUID$/P’  fstab    //显示以UUID结尾的行
sed  -n  ‘2,6P’  fstab             //显示2到6行
sed  -n  ‘2,6!P’  fstab            //不显示2到6行
sed  -n  ‘2p;6p’  fstab           //显示第2行和第6行

实验:
[root@client mnt]# cat -n fstab | sed -n '/^#/p'       //显示“#”开头的行

        #
        # /etc/fstab
        # Created by anaconda on Wed May  7 01:22:57 2014
        #
        # Accessible filesystems, by reference, are maintained under '/dev/disk'
        # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
        #
        UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
        /dev/vg0/vo /home   ext4    defaults    0 0
        #//172.25.254.226/student /mnt cifs defaults,username=student,password=123 0 0
[root@client mnt]#  sed -n '/^#/!p' fstab              //显示不以”#“开头的行

UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo /home   ext4    defaults    0 0
[root@client ~]# cat -n /etc/fstab | sed -n '2p;6p'     //显示第2,6行
     2  #
     6  # Accessible filesystems, by reference, are maintained under '/dev/dis  
[root@client ~]# cat -n /etc/fstab | sed -n '2,6p'      //显示第二行到第六行
     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'

这里写图片描述

2.2、d (删除)模式

sed  -n  ‘/^#/d’  fstab            //删除以“#”开头的行
sed  -n  ‘/UUID$/d’  fstab    //删除以UUID结尾的行
sed  -n  ‘2,6d’  fstab             //显示2到6行
sed  -n  ‘2,6!d’  fstab            //删除2到6行
sed  -n  ‘2p;6d’  fstab           //删除第2行和第6行

实验:
[root@client etc]# sed -e '2,6d' fstab       //删除第2到6行
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo /home   ext4    defaults    0 0
#//172.25.254.126/student /mnt cifs defaults,username=student,password=123 0 0
[root@client etc]# sed -e '/UUID$/d' fstab    //删除以UUID结尾的行
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo /home   ext4    defaults    0 0
#//172.25.254.126/student /mnt cifs defaults,username=student,password=123 0 0
[root@client etc]# sed -e '/^#/d' fstab      //删除以“#”开头的行
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo /home   ext4    defaults    0 0
[root@client etc]# sed -e '2p;6p' fstab      //删除第2行和第六
#
#
# /etc/fstab
# Created by anaconda on Wed May  7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 /                       xfs     defaults        1 1
/dev/vg0/vo /home   ext4    defaults    0 0
#//172.25.254.126/student /mnt cifs defaults,username=student,passw

这里写图片描述

2.3 、a(添加)模式

##换行添加!

sed  -e  ’/^UUID/ahello’  fstab                 //在以UUID开头的行后面体添加一行hello
sed  -e  ’/^UUID/ahello\nword’  fstab      //在以UUID开头的行后面体添加一行hello再换行添加world。

实验:
[root@client mnt]# cat westos 
hello 
[root@client mnt]#  sed -e '/hello/aworld' westos  //在hello行后添加world
hello 
world
[root@client mnt]#  sed -e '/hello/aworld\n123' westos //在hello行后添加world,再换行添加123
hello
world
123

这里写图片描述

2.4 、c(替换)模式

##在本行操作!

sed  -e  ‘/^UUID/chello’  fstab          v//将以UUID开头的行替换为hello

实验:
[root@client mnt]# cat westos  
hello
[root@client mnt]# sed -e '/hello/cworld\n123' westos    //将hello的行替换为world换行123
world
123

这里写图片描述

2.5 、w(写入)模式

sed  -e   ‘/^UUID/wfile.txt’  fstab                             //将fstab文件中以UUID开头的行写入file.txt文件中
sed  -n   ‘/^UUID/w /mnt/test’  fstab                       //同上,但-n没有输出,只显示处理结果
sed   ‘/^UUID/=’  fstab                                           //将文件fstab以UUID开头的行号输出,=表示加行号
sed   ‘1r /mnt/hello’   fstab                                         //将/mnt/hello文件写入fstab的第一行
sed  ‘$r /mnt/hello’   fstab                                         //将/mnt/hello文件写入fstab的最后一行

实验:
[root@client mnt]# sed -e '/hello/wfile' westos    //将westos文件中hello行写入file.t文件中

hello
[root@client mnt]# cat file 
hello
[root@client mnt]# cat file 
hello
world
[root@client mnt]# cat westos 
westos
[root@client mnt]# sed 'rwestos' file    //在file文件中的每一行添加westos文件
hello
westos
world
westos
[root@client mnt]# sed '1rwestos' file   //在file文件中的第一行添加westos文件
hello
westos
world
[root@client mnt]# sed '2rwestos' file   //在file文件中的第二行添加westos文件
hello
world
westos

这里写图片描述
这里写图片描述

2.6 、i(插入)模式

##在上一行进行插入!
实验:
[root@client mnt]# cat westos
westos
[root@client mnt]# sed -e '/westos/iworld' westos  
world
westos
[root@client mnt]# sed -e '/wes/iworld' westos 
world
westos
[root@client mnt]# sed -e '/w/iworld' westos 
world
westos
[root@client mnt]# sed -e '/et/iworld' westos   //由以上可以看出之可以匹配每行第一个字符,然后在上一行进行插入。
westos

这里写图片描述

3.其他参数

sed  -n  -f  prctise  fstab      // 对文件执行prctise的策略
/^UUID/p                               //文件内容
/^UUID/=                               //文件内容
sed  -n  -e ‘/^UUID/p’  -ne  ‘/^UUID/=’  fstab        //同上
sed  -n  -e  ‘/^UUID/p;/^UUID/=’  fstab
sed  ‘s/w/W/g’  fstab 将全文w换成W,与sed ‘s@w@W@g’ fstab        // 等同
sed  ‘1,3s/w/W/g ’ fstab 将前3行的w换成W
sed  ‘/adm/,/sync/s/nologin/westos/g’                   //passwd 替换adm到sync中间的nologin为westos
sed  ‘G’ fstab                   //G加空行,在每一行后面加空行
sed  ‘$!G’                          //fstab 在最后一行不加空行
sed  ‘!G’  fstab                  //不加空行
sed  ‘=’  fstab                  ////显示行号

实验:
[root@client mnt]# cp /etc/passwd . 
[root@client mnt]# vim passwd
[root@client mnt]# cat passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
sadm:x:3:4:adm:/var/adm:/sbin/nologin
p:x:4:7:lp:/var/spool/lpd:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1003:1003::/home/user3:/bin/bash
[root@client mnt]# sed 's/nologin/westos/g' passwd 
// 将文中所有出现的nologin替换为westos,不加/s时只替换每行出现的第一个字符.
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/westos
daemon:x:2:2:daemon:/sbin:/sbin/westos
sadm:x:3:4:adm:/var/adm:/sbin/westos
p:x:4:7:lp:/var/spool/lpd:/sbin/westos
polkitd:x:999:998:User for polkitd:/:/sbin/westos
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/westos
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1003:1003::/home/user3:/bin/bash

//将文件中第三到五行出现的nologin替换为westos

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/westos
sadm:x:3:4:adm:/var/adm:/sbin/westos
p:x:4:7:lp:/var/spool/lpd:/sbin/westos
polkitd:x:999:998:User for polkitd:/:/sbin/nologin
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1003:1003::/home/user3:/bin/bash
[root@client mnt]# sed '/bin/,/sadm/s/nologin/westos/g' passwd  
//将从bin开头的行到sadm结尾的行中的nologin替换为westos

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/westos
daemon:x:2:2:daemon:/sbin:/sbin/westos
sadm:x:3:4:adm:/var/adm:/sbin/nologin
p:x:4:7:lp:/var/spool/lpd:/sbin/nologin
polkitd:x:999:998:User for polkitd:/:/sbin/westos
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/sbin/nologin
user1:x:1001:1001::/home/user1:/bin/bash
user2:x:1002:1002::/home/user2:/bin/bash
user3:x:1003:1003::/home/user3:/bin/bash
[root@client mnt]# sed 's/nologin/westos/g;s/sbin/####/g' passwd   
//两个替换命令,1.将全文中的nologin替换为westos,2.将全文中的sbin替换为####

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/####/westos
daemon:x:2:2:daemon:/####:/####/westos
sadm:x:3:4:adm:/var/adm:/####/westos
p:x:4:7:lp:/var/spool/lpd:/####/westos
polkitd:x:999:998:User for polkitd:/:/####/westos
mysql:x:27:27:MariaDB Server:/var/lib/mysql:/####/westos

这里写图片描述
这里写图片描述
这里写图片描述

练习:写一个脚本,当执行该脚本时自动下载apache,同时修改用户输入端口号

[root@client mnt]# vim install_apache.sh 

#######################################
# Author:       yifan                 #
# Version:                            #
# Mail:                               #
# Date:         2018-28-06/22/18      #
# Description:                        #
#                                     #
#######################################

#!/bin/bash
yum install httpd.x86_64 -y;        //自动下载apache
sed -i "/^Listen/cListen $1" /etc/httpd/conf/httpd.conf;  //修改配置文件中端口号
echo ;
echo the listen is changed;     //输出修改成功
sed -ne '42p' /etc/httpd/conf/httpd.conf;   //显示配置文件中第42行
systemctl restart httpd;    //重启服务
[root@client mnt]# sh install_apache.sh 8080  //执行脚本,修改端口号为8080
Loaded plugins: langpacks
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-17.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================
 Package        Arch            Version               Repository          Size
===============================================================================
Installing:
 httpd          x86_64          2.4.6-17.el7          source7.0          1.2 M

Transaction Summary
===============================================================================
Install  1 Package

Total download size: 1.2 M
Installed size: 3.7 M
Downloading packages:
httpd-2.4.6-17.el7.x86_64.rpm                             | 1.2 MB   00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : httpd-2.4.6-17.el7.x86_64                                   1/1 
  Verifying  : httpd-2.4.6-17.el7.x86_64                                   1/1 

Installed:
  httpd.x86_64 0:2.4.6-17.el7                                                  

Complete!

the listen is changed   //修改成功
Listen 8080

这里写图片描述
这里写图片描述

猜你喜欢

转载自blog.csdn.net/yifan850399167/article/details/80777073
今日推荐