美文网首页
sed常用操作命令

sed常用操作命令

作者: sunnowyli | 来源:发表于2018-10-23 18:17 被阅读0次

sed: stream editor , 流/行 编辑器 ;

sed 命令详解:

sed [OPTIONS]...  {script-only-if-no-other-script}  [input-file]...

OPTIONS:

        -n, --quiet, --silent , suppress automatic printing of pattern space /禁止打印pattern space 内容

         -e script  ,--expression=script ,add the script to the commands to be executed ,支持多级编辑

          -f script-file, --file=script-file ,  add the contents of script-file to the commands to be executed,将命令汇总至script-file 方便执行

          -r, --regexp-extended ,use extended regular expressions in the script. 使用扩展正则表达式

           -i[SUFFIX], --in-place[=SUFFIX] : 直接编辑原文件

 script-only-if-no-other-script  :包括地址限定 和  编辑命令;

  地址限定:

        (1) #,# , 指定单行; $ : 最后一行

        (2) #1,+#, 指定多个单行

         (3)#,/pattern/         ## 匹配从第# 开始到第一次匹配到/pattern1/之间的行

         (4)/pattern1/, /pattern1/

        (5)first~step ,Match every step'th line starting with line first.

                eg: the address 2~5 will match every fifth line, starting with the second . 从第2行开始,每隔5行;

常见的编辑命令有:

         d  Delete pattern space. 删除pattern space 中的内容;

         p Print the current pattern space. .打印pattern space中的内容

        a \text  : append , 在匹配到的pattern行的下方追加一行

        i \text  :insert , 在匹配到的pattern行的上方插入一行

        r filename : Append text read from filename.  将filename 追加读入当前匹配到pattern的行的下方

        c \text ,Replace the selected lines with text ,替换匹配到pattern行为text ;

        w filename, Write the current pattern space to filename. 将匹配到pattern内容的行保存至filename中;

        s/regexp/replacement/opts : 将匹配到regexp 的内容替换为replacement

               其中,常用opts 有:

                        g: 全局替换

                        p: 显示替换成功的行

                        w  /PATH/FILENAME :  将替换成功的结果保存至指定文件中

 示例:

>>>sed -n '1,+4p' /etc/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

adm:x:3:4:adm:/var/adm:/sbin/nologin

lp:x:4:7:lp:/var/spool/lpd:/sbin/nologin

>>>sed '1,/^tome/ d' /etc/passwd

tom:x:4012:4012::/home/tom:/bin/bash

>>>sed -n -e '/^root/a \Superman' -e '/^root/i \superman' /etc/passwd

##删除/etc/init.d/functions 文件中所有以空白字符开头的行的行首的所有空白字符;

>>sed 's@^[[:space:]]\+@@g' /etc/init.d/functions

##删除/etc/fstab文件中所有以#开头的行的行首的# 号及#后面的所有空白字符;

>>>sed 's@^#\+[[:space:]]*@@g' /etc/fstab

相关文章

  • Linux 常用命令总结

    linux 命令速查手册linux 常用操作命令 sed 简明教程 awk 简明教程 常用命令 ls ...

  • sed常用操作命令

    sed: stream editor , 流/行 编辑器 ; sed 命令详解: sed [OPTIONS].....

  • 临时随笔sed/awk

    sed替换指定行内容 sed 命令行格式 脚本格式 基本操作命令 行定位 操作命令 实例 替换命令 高级操作命令 ...

  • LINUX sed命令的使用

    LINUX sed命令的使用 命令格式 sed常用命令 sed替换标记 sed元字符集 已匹配字符串标记& ⼦串匹...

  • sed命令的常用操作

    sed是一种流编辑器,它是文本处理中非常中的工具,能够完美的配合正则表达式使用,功能不同凡响。处理时,把当前处理的...

  • [2020春假]Linux下的文本操作(sed篇)

    Chapter4 sed替换命令详解 sed的替换命令是最常用的,也是讲解最多的。sed的模式空间 sed的基本工...

  • 文字处理

    Linux 下进行字符串操作最常用的三个命令: awk (检索) sed (编辑) grep (匹配) awk 语...

  • 【现学现忘&Shell编程】— 35.sed命令(一)

    1、sed命令说明 sed主要是用来将数据进行选取、替换、删除、新增的命令,grep,awk,sed称为是文本操作...

  • linux - sed常用命令

    linux - sed常用命令 sed命令用法[https://www.cnblogs.com/w1sh/p/14...

  • sed操作命令

    sed命令不会改变原文件的内容 基本操作命令: -a:新增行 nl passwd | sed '1,5a==...

网友评论

      本文标题:sed常用操作命令

      本文链接:https://www.haomeiwen.com/subject/mycizftx.html