美文网首页Linuxlinux
Linux12_ sed命令详解

Linux12_ sed命令详解

作者: Y大宽 | 来源:发表于2019-05-21 21:51 被阅读40次
  • sed意为“流编辑器(stream editor)”
    字符串筛选工具。
  • 对一个文档中的行进行操作

和vi编辑很相似。可以查看vim使用命令详解

1 sed基本格式

第一种

sed 参数 '脚本语句(/pattern/action)' 待操作文件

第二种

sed 参数  -f '脚本文件' 待操作文件

参数


sed参数

a 追加append
i 插入insert
d 删除delete
s替换substitution

$ cat seqtest
#! /bin/sh

echo "Enter a Yes or no"
read BUF

case "$BUF" in
   yes |Y|y|YES|Yes)
        echo "It's a yes.";;
      [nN]?)
          echo "It's a NO.";;
     *)
   echo "other case.";;
esac

echo "gong to return"
return 0
echo "going to return end"

2 行追加a

追加到后面

$ sed '4a this is an added line' seqtest

执行上面命令后,只会屏幕上输出,而不会保存到文件中

$ sed '4a this is an added line' seqtest
#! /bin/sh

echo "Enter a Yes or no"
read BUF
this is an added line

case "$BUF" in
   yes |Y|y|YES|Yes)
        echo "It's a yes.";;
      [nN]?)
          echo "It's a NO.";;
     *)
   echo "other case.";;
esac

echo "gong to return"
return 0
echo "going to return end"

如果想要保存那就需要加一个参数-i

$ sed -i '4a this is an added line' seqtest
$ cat seqtest
#! /bin/sh

echo "Enter a Yes or no"
read BUF
this is an added line

case "$BUF" in
   yes |Y|y|YES|Yes)
        echo "It's a yes.";;
      [nN]?)
          echo "It's a NO.";;
     *)
   echo "other case.";;
esac

echo "gong to return"
return 0
echo "going to return end"

3 删除某行

$ sed '5d' seqtest
#! /bin/sh

echo "Enter a Yes or no"
read BUF

case "$BUF" in
   yes |Y|y|YES|Yes)
        echo "It's a yes.";;
      [nN]?)
          echo "It's a NO.";;
     *)
   echo "other case.";;
esac

echo "gong to return"
return 0
echo "going to return end"

4 插入某行 和a用法一样,只是追加到行前

5 替换命令s

r$ sed 's/BUF/buffer/g' seqtest
#! /bin/sh

echo "Enter a Yes or no"
read buffer
this is an added line

case "$buffer" in
   yes |Y|y|YES|Yes)
        echo "It's a yes.";;
      [nN]?)
          echo "It's a NO.";;
     *)
   echo "other case.";;
esac

echo "gong to return"
return 0
echo "going to return end"

6常用sed命令

6.1打印包含特定字符的行

相当于grep命令

$ sed -n '/BUF/p' seqtest
read BUF
case "$BUF" in

如果不加-n是下面输出

$ sed '/BUF/p' seqtest
#! /bin/sh

echo "Enter a Yes or no"
read BUF
read BUF
this is an added line

case "$BUF" in
case "$BUF" in
   yes |Y|y|YES|Yes)
        echo "It's a yes.";;
      [nN]?)
          echo "It's a NO.";;
     *)
   echo "other case.";;
esac

echo "gong to return"
return 0
echo "going to return end"

6.2 某字符的特殊模式

$ sed  's/BUF/-$-/g' seqtest
#! /bin/sh

echo "Enter a Yes or no"
read -$-
this is an added line

case "$-$-" in
   yes |Y|y|YES|Yes)
        echo "It's a yes.";;
      [nN]?)
          echo "It's a NO.";;
     *)
   echo "other case.";;
esac

echo "gong to return"
return 0
echo "going to return end"

7 sed扩展sed -r

和grep不同,sed用参数r进行扩展

8 sed一次指定多条不同的替换命令,用";"隔开

9 明白下面例子

$ sed 's/<[^<>*]>//g' t.html
<html><head><title>Hello World</title></head>
<body>Welcome to the world of regexp!</body></html>
$ sed 's/<[^<>]*>//g' t.html
Hello World
Welcome to the world of regexp!

相关文章

  • Linux12_ sed命令详解

    sed意为“流编辑器(stream editor)”字符串筛选工具。对一个文档中的行进行操作 和vi编辑很相似。可...

  • sed常用操作命令

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

  • Linux-sed-1

    #############20190820- sed命令用法详解 sed命令用法 sed是一种流编辑器,它是文本处...

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

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

  • sed命令详解

    原文链接:sed命令_Linux sed 命令用法详解:功能强大的流式文本编辑器 http://man.linu...

  • Linux sed命令实例详解

    Linux sed命令实例详解 功能说明:利用script来处理文本文件。 语法:sed [-hnV][-e][-...

  • SED命令详解

    sed命令用来对文本文件行进行处理; 格式 命令行选项 通过示例学习

  • sed命令详解

    一、简介 Sed是stream editor的简称,也就是流编辑器。它一次处理一行内容,处理时,把当前处理的行存储...

  • sed 命令详解

    sed 即 stream editor,一个简单而强大的文本解析转换工具,1973-1974年期间由贝尔实验室的 ...

  • sed命令详解

    1. 简介 sed命令是一个很强大的文本编辑器,可以对来自文件、以及标准输入的文本进行编辑。 执行时,sed会从文...

网友评论

    本文标题:Linux12_ sed命令详解

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