简体   繁体   English

使用Sed替换用大括号分隔的多行内容

[英]Sed to replace content across multiple lines delimited by braces

I'm looking for a sed function that would be able to do find a set of lines and replace it with another set using delimiters. 我正在寻找一个sed函数,它将能够找到一组行,并使用定界符将其替换为另一组行。

Example

Source: 资源:

class Requests
{
  function approveRequests($users){
    foreach($users as $user){
      if(hasQualified($user)){
         acceptUser($user);
      }else{
         rejectUser($user)
      }
   }
 } 

} }

To :

class Requests
{
  function approveRequests($users){
    autoAcceptMember($users);
  } 
}

Please note the the source might have several sets of other braces but they will be equally matched. 请注意,来源中可能有几组其他大括号,但它们将相等地匹配。 so it there are 3 opening, then there will be 3 closing and so I'd like to get everything in-between 所以它有3个开盘,然后会有3个闭盘,所以我想让所有东西都处于中间

sed is not able to do the above it operates on a single lines only. sed无法完成上述操作,它只能在一行上运行。

(OK, it can operate on multiple lines, but it can't distinguish between } that look the same). (好的,它可以在多行上操作,但是不能区分看起来相同的} )。

You will have to use eg awk or other more advanced language (python, perl, etc.) 您将不得不使用例如awk或其他更高级的语言(python,perl等)

This will probably give an idea on how to edit your file with sed. 这可能会给您一个关于如何使用sed编辑文件的想法。

cat inputFile.txt | sed -n '/^class Requests/,/^}/ p' | sed -e '/foreach/,$ d' | sed -e 's/\(function.*\)/\1\n    autoAcceptMember($users);\n  }\n}\n/'

Output: 输出:

class Requests
{
  function approveRequests($users){
    autoAcceptMember($users);
  }
}

inputFile.txt: inputFile.txt:

class here
{
    are
    some extra lines
} 

class Requests
{
  function approveRequests($users){
    foreach($users as $user){
      if(hasQualified($user)){
         acceptUser($user);
      }else{
         rejectUser($user)
      }
   }
 } 

}

class here2 {
    function are ()  {
        some extra lines 
        also
}

Use sed and match with regex and print the lines between 使用sed并与正则表达式匹配并打印之间的行

> cat inputFile.txt | sed -n '/^class Requests/,/^}/ p'
class Requests
{
  function approveRequests($users){
    foreach($users as $user){
      if(hasQualified($user)){
         acceptUser($user);
      }else{
         rejectUser($user)
      }
   }
 }

}

Use sed and match with regex and delete the lines between 使用sed并与正则表达式匹配并删除之间的行

sed -e '/foreach/,$ d' will give the following, in context:
> cat inputFile.txt | sed -n '/^class Requests/,/^}/ p' | sed -e '/foreach/,$ d'
class Requests
{
  function approveRequests($users){

Use sed and match with regex, substitute and add tagged match 使用sed并与正则表达式匹配,替换并添加带标签的匹配项

sed -e 's/\(function.*\)/\1\n    autoAcceptMember($users);\n  }\n}\n/' will give the following, in context:
> cat inputFile.txt | sed -n '/^class Requests/,/^}/ p' | sed -e '/foreach/,$ d' | sed -e 's/\(function.*\)/\1\n    autoAcceptMember($users);\n  }\n}\n/'
class Requests
{
  function approveRequests($users){
    autoAcceptMember($users);
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM