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