sed - 在匹配后的X行之后插入行

时间:2022-11-18 21:49:02

I have the following contents:

我有以下内容:

void function_1()
{
    //something (taking only 1 line)
}
->INSERT LINE HERE<-
//more code

Using sed, I want to insert line at the INSERT LINE HERE label. The easiest way should be:

使用sed,我想在INSERT LINE HERE标签处插入行。最简单的方法应该是:

  1. find text "function_1"
  2. 找到文字“function_1”

  3. skip 3 lines
  4. 跳过3行

  5. insert new line
  6. 插入新行

But none of the known sed options do the job.

但是没有一种已知的sed选项可以胜任这项工作。

sed '/function_1/,3a new_text

inserts new_text right after 'function_1'

在'function_1'之后插入new_text

sed '/function_1/,+3a new_text

inserts new_text after each of the next 3 lines, following 'function_1'

在'function_1'之后,在接下来的3行中的每一行之后插入new_text

sed '/function_1/N;N;N; a new_text

inserts new_text at multiple places, not related to the pattern

在多个位置插入new_text,与模式无关

Thanks.

4 个解决方案

#1


5  

Try this:

sed "/function_1/{N;N;N;a new_text
}" filename

#2


2  

sed '/function_1(/,/^[[:space:]]*}/ {
 ,/^[[:space:]]*}/ a\
Line that\
you want to\
insert (append) here
   }' YourFile
  • insert the line after the } (alone in the line with eventually some space before) from the section starting with function_1(
  • 在从function_1开始的部分中插入}之后的行(单独在最后一行空格的行中)

  • i assume there is no } alone in your internal code like in your sample
  • 我假设您的内部代码中没有像您的样本中那样单独使用}

be carreful on selection based on function name because it could be used (and normaly it is) as a call to the function itself in other code section so maybe a /^void function_1()$/ is better

在基于函数名称的选择上是可靠的,因为它可以在其他代码段中作为函数本身的调用使用(并且通常是它)所以可能是/ ^ void function_1()$ /更好

#3


1  

Use awk:

awk '1;/function_1/{c=4}c&&!--c{print "new text"}' file
  • 1 is a shorthand for {print}, so all lines in the file are printed
  • 1是{print}的简写,因此打印文件中的所有行

  • when the pattern is matched, set c to 4
  • 匹配模式时,将c设置为4

  • when c reaches 1 (so c is true and !--c is true), insert the line
  • 当c达到1(所以c为真并且! - c为真)时,插入该行

You could just use !--c but adding the check for c being true as well means that c doesn't keep decreasing beyond 0.

你可以使用! - c但是加上c为真的检查也意味着c不会继续减少超过0。

#4


0  

Don't count, match:

不计,匹配:

sed -e '/^void function_1()/,/^}$/ { /^}$/a\
TEXT TO INSERT
}' input

This looks at the block between the declaration and the closing brace, and then append TEXT_TO_INSERT after the closing brace.

这将查看声明和右大括号之间的块,然后在右大括号后附加TEXT_TO_INSERT。

#1


5  

Try this:

sed "/function_1/{N;N;N;a new_text
}" filename

#2


2  

sed '/function_1(/,/^[[:space:]]*}/ {
 ,/^[[:space:]]*}/ a\
Line that\
you want to\
insert (append) here
   }' YourFile
  • insert the line after the } (alone in the line with eventually some space before) from the section starting with function_1(
  • 在从function_1开始的部分中插入}之后的行(单独在最后一行空格的行中)

  • i assume there is no } alone in your internal code like in your sample
  • 我假设您的内部代码中没有像您的样本中那样单独使用}

be carreful on selection based on function name because it could be used (and normaly it is) as a call to the function itself in other code section so maybe a /^void function_1()$/ is better

在基于函数名称的选择上是可靠的,因为它可以在其他代码段中作为函数本身的调用使用(并且通常是它)所以可能是/ ^ void function_1()$ /更好

#3


1  

Use awk:

awk '1;/function_1/{c=4}c&&!--c{print "new text"}' file
  • 1 is a shorthand for {print}, so all lines in the file are printed
  • 1是{print}的简写,因此打印文件中的所有行

  • when the pattern is matched, set c to 4
  • 匹配模式时,将c设置为4

  • when c reaches 1 (so c is true and !--c is true), insert the line
  • 当c达到1(所以c为真并且! - c为真)时,插入该行

You could just use !--c but adding the check for c being true as well means that c doesn't keep decreasing beyond 0.

你可以使用! - c但是加上c为真的检查也意味着c不会继续减少超过0。

#4


0  

Don't count, match:

不计,匹配:

sed -e '/^void function_1()/,/^}$/ { /^}$/a\
TEXT TO INSERT
}' input

This looks at the block between the declaration and the closing brace, and then append TEXT_TO_INSERT after the closing brace.

这将查看声明和右大括号之间的块,然后在右大括号后附加TEXT_TO_INSERT。