【问题标题】:How to delete pattern which matches with the next new pattern which is calling in the next line by using /bin/sed command in dc_shell如何使用 dc_shell 中的 /bin/sed 命令删除与下一行调用的下一个新模式匹配的模式
【发布时间】:2018-08-08 04:55:38
【问题描述】:

下面是我的文件示例,

         signal {
          {
            ab
            cd
          }

        "m_1_clk" {
              P {
                 '0ns' D;
                  '25ns' U;
                  '65ns' D;
                  }
             }

            "m_0_clk" {
                        P {
                          '0ns' D;
                          '25ns' U;
                          '65ns' D;
                          }
                       }

              "o_[9]" {
                     Data S Mk {
                     Active Up;
                            }
                        }

               "m_0_clk" {
                          Data S Mk {
                            Active Up;
                            }
                        }

             "m_1_clk" {
                          Data S Mk {
                          Active Up;
                            }
                        }
            }

我希望上述文件的输出是:

      signal {
           {
              ab
              cd
            }
         "o_[9]" {
                  Data S Mk {
                  Active Up;
                      }
                   }
              }     

嗨, 以上是我的文件示例 我想删除与下一行调用的下一个模式匹配的模式。

尝试搜索特定模式以删除它不应该出现在我的文件中的行。

需要在下一行用新模式搜索特定模式。因为第一行在多个循环中调用。所以应该grep所有内容的特定模式。 我尝试在 tcl 中使用以下命令,但它不起作用。谁能帮我解决这个问题,

下面是tcl中使用的sed命令

exec /bin/sed -e {s/\m\(.*\)clk" {\.*\n\.*Data\.*//}  -i file

exec /bin/sed -e {s/\m\(.*\)clk" {\.*\n\.*P {\.*//}  -i  file

【问题讨论】:

  • 我认为我们需要一些期望的输出。我还认为regex 可能不是这项工作的工具,因为这看起来像是上下文搜索。
  • 请解释保留“o_[9]”块但丢弃“m_*_clk”块的标准。
  • 添加了一些逻辑并在设计中创建了一些端口以进行一些修改,但在设计结束时我试图删除我在设计中创建的端口。所以只需要“m_*_clk”块

标签: perl sed tcl


【解决方案1】:

使用上下文相关解析器的解决方案:

use Marpa::R2 qw();

my $input = <<'INPUT';
         signal {
          {
            ab
            cd
          }

        "m_1_clk" {
              P {
                 '0ns' D;
                  '25ns' U;
                  '65ns' D;
                  }
             }

            "m_0_clk" {
                        P {
                          '0ns' D;
                          '25ns' U;
                          '65ns' D;
                          }
                       }

              "o_[9]" {
                     Data S Mk {
                     Active Up;
                            }
                        }

               "m_0_clk" {
                          Data S Mk {
                            Active Up;
                            }
                        }

             "m_1_clk" {
                          Data S Mk {
                          Active Up;
                            }
                        }
            }
INPUT

my $grammar = Marpa::R2::Scanless::G->new({
    bless_package => 'Foo',
    source => \<<'GRAMMAR',
        :default ::= action => [values] bless => ::lhs
        lexeme default = action => [ value ] bless => ::name latm => 1
        :start ::= blocks
        blocks ::= block+
        block ::= optionalname [{] contentblocks [}] optionalws
        optionalname ::= name | epsilon
        contentblocks ::= contentorblock+
        contentorblock ::= content | block
        name ~ [a-zA-Z0-9_"\x{5b}\x{5d} ]+
        content ~ [a-zA-Z0-9;'\s]*
        optionalws ~ [\s]*
        epsilon ::=
GRAMMAR
});

my $parser = Marpa::R2::Scanless::R->new({grammar => $grammar});
$parser->read(\$input);
my $output;
deleteblocks($parser->value->$*);
print $output;

sub deleteblocks {
    my ($v) = @_;
    return if ref $v eq 'Foo::block' and $v->[0][0]
        and $v->[0][0][0] !~ /signal|"o_\[9\]"/;
    if (ref $v) {
        deleteblocks($_) for $v->@*;
    } else {
        $output .= $v || '';
    }
}

输出是:

     signal {
      {
        ab
        cd
      }

    "o_[9]" {
                 Data S Mk {
                 Active Up;
                        }
                    }

           }

【讨论】:

    【解决方案2】:

    您能否尝试关注并让我知道这是否对您有帮助。(考虑到您的实际 Input_file 与显示的示例文件相同)

    awk -v line_number=$(cat Input_file | wc -l) '/signal {/ || /"o_\[9\]" {/ || FNR==line_number{flag=1}; /^$/{flag=""} flag'    Input_file
    

    说明:现在也添加代码说明如下:

    awk -v line_number=$(cat Input_file | wc -l) '   ##Creating variable named line_number in awk script whose value is number of lines of Input_file.
    /signal {/ || /"o_\[9\]" {/ || FNR==line_number{ ##checking condition here if a line contains strings either signal {  OR "o_\[9\]" { then do following:
      flag=1}                                        ##Setting variable named flag here and setting its value to 1 here.
    /^$/{                                            ##Checking condition here if a line is NULL or BLANK then do following:
      flag=""}                                       ##Nullifying the variable flag here.
    flag                                             ##awk works on method of condition then actions, so checking if variable flag value is NOT NULL here and not mentioning any action here so by default peint of current line will happen here.
    '   Input_file                                   ##Metioning the Input_file name here.
    

    【讨论】:

    • 您忘记了 signal 块中的最后一个 }。
    • @daxim,谢谢 TON 先生,我现在编辑了我的一个班轮,很快就会编辑解释部分。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-01
    • 2015-05-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多