四时宝库

程序员的知识宝库

sed命令教程:sed在匹配之前插入行

sed命令,支持在匹配行之前插入一行,我们可以使用sed 动作参数:i,在匹配行之前添加新行。

语法

  sed '/string/i newLine' file       #option: 需要匹配的字符串
  sed 'ni newLine' file              #n: 行号
  sed '$i newLine' file              #$: 最后一行


示例


使用sed 动作参数:i ,在匹配行之前添加新行;

在下面的示例中,我们将使用sed i 动作参数,在包含字符串“ hello”的行之前添加新行:

  ~ sed '/hello/ised add a new line' test.log


在第一行之前插入新行:

  ~ sed '1ised add a new line' test.log


在最后一行之前插入新行:

  ~ sed '$ised add a new line' test.log
this is a test file.
hello
apple
watch
sed add a new line
world


在每行之前插入新行:

  ~ sed '/$/i sed add a new line' test.log


参考

GNU sed 语法和sed 示例

sed命令教程:sed 地址和地址范围(选择行)及应用示例

发表评论:

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言
    友情链接