将模式从特定行移动到该规则的末尾 [英] Move a pattern from specific line to end of that rule

查看:47
本文介绍了将模式从特定行移动到该规则的末尾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何将放置在某行之后的模式移动到规则的另一个位置.

I am wondering how can I move a pattern placed after certain line to another location of a rule.

考虑下面的例子,

rule tmap {

  hideON 
   student_analysis Sam -expr "marks(Sam)" -gt 0
  hideOFF

  -Designate [ School = "ABC]
  -Designate [ Name = "Sam" ]
  -Designate [ Country= "ABC]
  -Designate [ State = "Sam"]
  -Designate [ ROLL Number= "Sam"]
}

其中 hideOFF 需要从 student_analysis 移动到结尾大括号之前.输出文件应该有类似

where hideOFF needs to be moved from student_analysis to just before the end curly braces. Output file should have something like

rule tmap {

      hideON 
       student_analysis Sam -expr "marks(Sam)" -gt 0


      -Designate [ School = "ABC]
      -Designate [ Name = "Sam" ]
      -Designate [ Country= "ABC]
      -Designate [ State = "Sam"]
      -Designate [ ROLL Number= "Sam"]
     hideOFF
    }

只是添加这些 -Designate 条目可能在一行中,因此行数不应成为标准.我的输入文件可能像

Just to add these -Designate entries might be in a single row so number of row should not be a criterion. I might have my input file like

 rule tmap {

      hideON 
       student_analysis Sam -expr "marks(Sam)" -gt 0
      hideOff

      -Designate [ School = "ABC] -Designate [ Name = "Sam" ] -Designate [ Country= "ABC] -Designate [ State = "Sam"] -Designate [ ROLL Number= "Sam"]

    }

如果您需要更多说明,请告诉我.

In case if you need more clarification, please let me know.

最终解决方案基于 Peters 帮助

Final solution Based on Peters help

#!/usr/bin/tclsh
set fileData [lindex $argv 0 ]
set fp [open $fileData r]
set data [read $fp]
set lines [split [string trim $data] \n]
set hideoff {}
foreach line $lines {
  if {[regexp {^\s*hideoff\s*} $line match ] == 1 } {
        set hideoff $line
   } elseif {[regexp {^\s*\}\s*} $line match ] == 1 } {
        puts $hideoff
        puts $line
    } else {
        puts $line
    }
}

谢谢大家这么好的建议.

Thanks everyone for so nice suggestions.

推荐答案

(Tcl 解决方案)您可以通过重新打印行来做到这一点.你先看看它们,跳过你想移动的那一行,然后当你看到你之前想插入的行时打印出来.

(Tcl solution) You can do that by reprinting lines. You look at them first, skip the line you want to move and then print it when you see the line you want it inserted before.

set lines [split [string trim $data] \n]

set hideoff {}
foreach line $lines {
    if {[string trim $line] eq "hideOFF"} {
        set hideoff $line
    } elseif {[string trim $line] eq "\}"} {
        puts $hideoff
        puts $line
    } else {
        puts $line
    }
}

另一个 Tcl 解决方案:

Another Tcl solution:

regsub -all {(\s*hideOFF)([^\}]+)} $data \\2\\1\n

两种解决方案都适用于多个块.

Both solutions work for multiple blocks.

文档:eq(运算符),foreach,if,puts,regsub,设置,拆分,字符串,Tcl 正则表达式语法

Documentation: eq (operator), foreach, if, puts, regsub, set, split, string, Syntax of Tcl regular expressions

这篇关于将模式从特定行移动到该规则的末尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆