Chinaunix首页 | 论坛 | 博客
  • 博客访问: 96880
  • 博文数量: 34
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 350
  • 用 户 组: 普通用户
  • 注册时间: 2014-07-04 22:47
文章分类

全部博文(34)

文章存档

2017年(34)

我的朋友

分类: 服务器与存储

2017-04-07 22:55:40

编辑命令:
        d:删除
        p:打印
        i    \text
        a   \text
        r    /path/to/somefile:在指定位置把另外一个文件的内容插入进来
        w   /path/to/somefile:将符合条件的所有行保存至指定的文件中
        =: 显示符合条件的行的行号
        s///:查找条件可以使用模式,但要替换的内容不行

地址定界:
        startline,endline
                1,3
         /pat1/,/pat2/
         /pattern/

用法: sed [option] 'addr1[,addr2]编辑命令 ' FILE
          sed [option] "addr1[,addr2]编辑命令" FILE
          选项:
              -n:静默模式,不输出模式空间的内容:
              -r:扩展的正则表达式
              -f /path/to/sed_script_file:指定sed脚本文件
             -e `script' -e 'script':指定多个编辑指令          

一、删除命令

sed 'd'
1、删除/etc/fstab的第一行到第三行
[root@localhost ~]# sed '1,3d' /etc/fstab
# Created by anaconda on Tue Mar 21 07:38:31 2017
#
# 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=8b0e83ad-60ca-43cc-a356-7c8f9c86497b /                       ext4    defaults        1 1
UUID=91705c7f-8b89-4eef-b281-b66d66900451 /boot                   ext4    defaults        1 2
UUID=44173a75-09b4-466c-b1c7-8202c571f04f /opt                    ext4    defaults        1 2
UUID=7f07495a-9e48-49df-83fb-e7c81f4e318e swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

[root@localhost ~]# sed -n  '1,3d' /etc/fstab

2、删除所有以#号开头的行

[root@localhost ~]# sed '/^#/d' /etc/fstab

UUID=8b0e83ad-60ca-43cc-a356-7c8f9c86497b /                       ext4    defaults        1 1
UUID=91705c7f-8b89-4eef-b281-b66d66900451 /boot                   ext4    defaults        1 2
UUID=44173a75-09b4-466c-b1c7-8202c571f04f /opt                    ext4    defaults        1 2
UUID=7f07495a-9e48-49df-83fb-e7c81f4e318e swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

3、删除以斜线开头的行
[root@localhost ~]# sed '/^\//d' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Mar 21 07:38:31 2017
#
# 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=8b0e83ad-60ca-43cc-a356-7c8f9c86497b /                       ext4    defaults        1 1
UUID=91705c7f-8b89-4eef-b281-b66d66900451 /boot                   ext4    defaults        1 2
UUID=44173a75-09b4-466c-b1c7-8202c571f04f /opt                    ext4    defaults        1 2
UUID=7f07495a-9e48-49df-83fb-e7c81f4e318e swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

二、显示打印命令

三、添加或追加

1、在以#开头的行后加上hello 和line两个词

[root@localhost ~]# sed '/^#/a \hello /line' /etc/fstab

#
hello /line
# /etc/fstab
hello /line
# Created by anaconda on Tue Mar 21 07:38:31 2017
hello /line
#
hello /line
# Accessible filesystems, by reference, are maintained under '/dev/disk'
hello /line
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
hello /line

2、在以#开头的行前面加上hello 和line两个词

[root@localhost ~]# sed '/^#/i \hello /line' /etc/fstab

hello /line
#
hello /line
# /etc/fstab
hello /line
# Created by anaconda on Tue Mar 21 07:38:31 2017
hello /line
#
hello /line
# Accessible filesystems, by reference, are maintained under '/dev/disk'
hello /line
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
hello /line
#

3、在以大写字母开头的行后追加first line和second line 两行字符
[root@localhost ~]# sed '/^[[:upper:]]/a \first line. \nsecond line.' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Mar 21 07:38:31 2017
#
# 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=8b0e83ad-60ca-43cc-a356-7c8f9c86497b /                       ext4    defaults        1 1
first line.
second line.
UUID=91705c7f-8b89-4eef-b281-b66d66900451 /boot                   ext4    defaults        1 2
first line.
second line.
UUID=44173a75-09b4-466c-b1c7-8202c571f04f /opt                    ext4    defaults        1 2
first line.
second line.
UUID=7f07495a-9e48-49df-83fb-e7c81f4e318e swap                    swap    defaults        0 0
first line.
second line.

4、把/etc/issue该文件插入到/etc/fstab下以UUID开头的行的下面

[root@localhost ~]# sed '/^UUID/r /etc/issue' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Mar 21 07:38:31 2017
#
# 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=8b0e83ad-60ca-43cc-a356-7c8f9c86497b /                       ext4    defaults        1 1
CentOS release 6.5 (Final)
Kernel \r on an \m

UUID=91705c7f-8b89-4eef-b281-b66d66900451 /boot                   ext4    defaults        1 2
CentOS release 6.5 (Final)
Kernel \r on an \m

UUID=44173a75-09b4-466c-b1c7-8202c571f04f /opt                    ext4    defaults        1 2
CentOS release 6.5 (Final)
Kernel \r on an \m

UUID=7f07495a-9e48-49df-83fb-e7c81f4e318e swap                    swap    defaults        0 0
CentOS release 6.5 (Final)
Kernel \r on an \m

tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0

5、将/etc/fstab里以#号开头的行,保存至/tmp/filetable.txt里

[root@localhost ~]# sed '/^#/w /tmp/filetable.txt' /etc/fstab

#
# /etc/fstab
# Created by anaconda on Tue Mar 21 07:38:31 2017
#
# 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=8b0e83ad-60ca-43cc-a356-7c8f9c86497b /                       ext4    defaults        1 1
UUID=91705c7f-8b89-4eef-b281-b66d66900451 /boot                   ext4    defaults        1 2
UUID=44173a75-09b4-466c-b1c7-8202c571f04f /opt                    ext4    defaults        1 2
UUID=7f07495a-9e48-49df-83fb-e7c81f4e318e swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
[root@localhost ~]# cat /tmp/filetable.txt
#
# /etc/fstab
# Created by anaconda on Tue Mar 21 07:38:31 2017
#
# 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
#






阅读(1450) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~