linux-sed(—) sed的基本语法和使用

1:sed 的作用 主要用来自动编辑一个或者多个文件,简化对于文件的反复操作 过滤等

2:语法的格式

     sed  [options] '处理动作' 文件名   必须使用 ' ' 引用操作

3:常用选项

     选项                        作用

     -e                   进行多次编辑

     -n                   取消默认输出 (常用)

     -r                   使用扩展正则表达式  (常用)

     -i                   原地编辑 (修改源文件)  (常用)

     -f                  指定sed脚本的文件名

4:常见处理动作

   动作                         说明

    'p'                             打印

    'i'                             指定行之前插入

    'a'                           指定行之后插入

    'c'                           替换指定行所有内容

    'd'                           删除指定行

举例:操作文件内容

root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
lp:x:4:adm:/var/adm:/sbin/nologin
192.168.195.12
172.18.16.254

5:打印文件内容

 5.1: 对于test8.txt文件不做任何操作

 sed  ' '            test8.txt

5.2:打印每一行 并取消默认输出

sed -n 'p'      test8.txt

5.3:打印第一行

sed -n '1p' test8.txt

5.4:打印1-4行

sed  -n  '1,5p' test8.txt

5.5:打印最后一行

sed -n '$p' test8.txt

6:增加文件内容

  6.1:在文件最后一行增加

      sed       '$ahello '          test8.txt

  6.2:在文件每一行增加

      sed       ' ahello'          test8.txt

  6.3:在文件第4行增加

      sed       ' 5ahello'          test8.txt

  6.4:在文件最后一行上一行增加

      sed       ' $ihello'          test8.txt

  6.5:在文件第6行上一行增加

      sed       ' 6ihello'          test8.txt

6.5:以文件good开头的上一行添加

      sed       '/^good/ihello'

7:替换指定的内容 是指整行替换

   7.1:替换文件第3行内容

       sed  '3cgood'  test8.txt

   7.2:替换文件每行内容

       sed  'cgood'  test8.txt 

   7.3:替换文件第1-3行内容

       sed  '1,3cgood'  test8.txt

   7.4:替换文件以user开头的内容

       sed  '/^user/cgood'  test8.txt

8:删除指定的内容 是指整行删除

    8.1:删除文件第3行内容

       sed  '3d'  test8.txt

   8.2:删除文件每行内容

       sed  'd'  test8.txt 

   8.3:删除文件第1-3行内容

       sed  '1,3d'  test8.txt

   8.4:删除文件以user开头的内容

       sed  '/^user/d'  test8.txt

9:其他命令

    命令                说明

     r             从另外文件中读取你内容

     w             内容另存为

     &            保存查找串以便在替换串中引用

     =            打印行号

     !            是取反的意思

     9.1:从/etc/profile文件中读取第3行的内容 在2.txt找到该行打印

          sed  '3r /home/shell'   2.txt

    9.2:在2.txt中查找root行内容 写入到/etc/profile  文件中

          sed  '/root/w /home/shell''   2.txt

    9.3:在2.txt中查找出现数字最多4次的行内容 写入到/etc/profile  文件中

          sed  '/[0-9]{4}/w /home/shell''   2.txt

10:对于文件进行搜素替换 (分隔符可以自己指定)

       10.1:在/home/shell/test8.txt 中 搜素root 将其替换成ROOT

         sed -n 's/root/ROOT/' /home/shell/test8.txt

      10.2:在/home/shell/test8.txt 中 搜素所有的root 将其替换成ROOT

         sed -n 's/root/ROOT/g' /home/shell/test8.txt    

      10.3:在/home/shell/test8.txt 中 搜素所有的root 将其替换成ROOT

         sed -n 's@/root@ROOT@g' /home/shell/test8.txt

11: 重点常用的 **********************

     11.1搜素test8.txt文件中1到5行中以空格开头的替换成 #号 并且输出行号

       sed  -n '1,5s/^/#'  /home/shell/test8.txt   //注释行

     11.2搜素test8.txt文件中1到5行中以#开头的替换成空格 并且输出行号

       sed  -n '1,5s/^#/'  /home/shell/test8.txt   // 放开行

 

      

    

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值