sed在某一行行首添加字符_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
8d4da518f7a84e25fe4b9e93d01ef46f.png

在第一行之前插入新行:

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

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

➜  ~ sed '$ised add a new line' test.logthis is a test file.helloapplewatchsed add a new lineworld

在每行之前插入新行:

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

参考

GNU sed 语法和sed 示例

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


版权声明:本文为weixin_39754915原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。