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.logthis is a test file.helloapplewatchsed add a new lineworld
在每行之前插入新行:
➜ ~ sed '/$/i sed add a new line' test.log
参考
GNU sed 语法和sed 示例
sed命令教程:sed 地址和地址范围(选择行)及应用示例