我可以在功能名称&之后插入文字吗?打开括号
例如:
void func()
{
Some text needs to be inserted before me
}
O / P
void func()
{
Hello here I am...
Some text needs to be inserted before me
}
使用awk / sed?
答案 0 :(得分:0)
这可能适合你(GNU sed):
sed '/^void func()/!b;n;a\Hello here I am...' file
如果它与func
名称不匹配,则会失效。匹配后打印该行,读取下一行并将所需的字符串追加到{
之后打印。
使用以下方法可以实现同样的目标:
sed -e '/^void func()/{n;a\Hello here I am...' -e '}' file
答案 1 :(得分:0)
sed是针对单个行的简单替换,因为你应该使用awk更有趣。例如,将GNU awk用于多字符RS,请注意脚本如何工作,而不管这些常见的函数定义布局可能性如何:
$ cat file
void func()
{
Some text needs to be inserted before me
}
void
func()
{
Some text needs to be inserted before me
}
void func() {
Some text needs to be inserted before me
}
$ awk -v RS='^$' -v ORS= '{gsub(/void\s+func\(\)\s*{/,"&\nHello here I am...")}1' file
void func()
{
Hello here I am...
Some text needs to be inserted before me
}
void
func()
{
Hello here I am...
Some text needs to be inserted before me
}
void func() {
Hello here I am...
Some text needs to be inserted before me
}