需要Groovy方法来完成部分文件替换 [英] Need the Groovy way to do partial file substitutions

查看:301
本文介绍了需要Groovy方法来完成部分文件替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要修改的文件。我需要修改的部分(不是整个文件),与下面显示的属性类似。问题是,如果你愿意,我只需要替换值的一部分,即ConfigurablePart。我收到这个文件,所以无法控制它的格式。

  alpha.beta.gamma.1 = constantPart1ConfigurablePart1 
alpha。 beta.gamma.2 = constantPart2ConfigurablePart2
alpha.beta.gamma.3 = constantPart3ConfigurablePart3

I这样做的工作,但我知道这是非常糟糕的!

  def updateFile(String pattern,String updatedValue){
def myFile = new File(。,inputs /fileInherited.txt)
StringBuffer updatedFileText = new StringBuffer()
def ls = System.getProperty('line.separator')
myFile.eachLine {line - >
def regex = Pattern.compile(/ $ pattern /)
def m =(line =〜regex)
if(m.matches()){
def buf = new StringBuffer(line)
buf.replace(m.start(1),m.end(1),updatedValue)
line = buf.toString()
}
println line
updatedFileText.append(line).append(ls)
}
myFile.write(updatedFileText.toString())
}

传入的模式需要包含一个在StringBuffer中被替换的组。有人知道这应该如何在Groovy中完成吗?

编辑 - 定义预期输出

包含示例行需要更新,以便每行的ConfigurablePart被替换为提供的更新文本。对于我的丑陋解决方案,我需要调用方法3次,一次替换ConfigurablePart1,一次替换ConfigurablePart2,最后替换ConfigurablePart3。这可能是一个更好的方法!



* 更新 - 做了我真正需要的答案*



如果其他人遇到过类似的问题,我问到的Groovy代码改进最能体现在接受的答案中。但是,对于我的问题,并没有完全解决我的问题。因为我只需要替换一部分匹配的行,所以我需要使用反向引用和组。我可以做这项工作的唯一方法是定义一个由三部分组成的regEx,如:

 (。*)(matchThisPart)( 。*)

完成后,我可以使用:

  it.replaceAdd(〜/ $ pattern /,\ $ 1 $ replacement \ $ 3)

感谢这两个回复 - 每个帮助我都得到了很多!

解决方案

使用闭包作为参数可以使它更加冗长。这是如何做到的:

  // abc.txt 
abc.item.1 = someDummyItem1
abc.item.2 = someDummyItem2
abc.item.3 = someDummyItem3
alpha.beta.gamma.1 = constantPart1ConfigurablePart1
alpha.beta.gamma.2 = constantPart2ConfigurablePart2
alpha.beta.gamma.3 = constantPart3ConfigurablePart3
abc.item.4 = someDummyItem4
abc.item.5 = someDummyItem5
abc.item.6 = someDummyItem6


$ b Groovy代码:

  / /替换文件中的模式并顺序写入文件。 
def replacePatternInFile(file,Closure replaceText){
file.write(replaceText(file.text))
}

def file = new File('abc。 txt')
def patternToFind =〜/ ConfigurablePart /
def patternToReplace ='NewItem'

//调用方法
replacePatternInFile(file){
it.replaceAll(patternToFind,patternToReplace)
}

println file.getText()

//打印:
abc.item.1 = someDummyItem1
abc.item.2 = someDummyItem2
abc.item.3 = someDummyItem3
alpha.beta.gamma.1 = constantPart1NewItem1
alpha.beta.gamma.2 = constantPart2NewItem2
alpha.beta.gamma.3 = constantPart3NewItem3
abc.item.4 = someDummyItem4
abc.item.5 = someDummyItem5
abc.item.6 = someDummyItem6

确认文件 abc.txt 。我没有像你所做的那样使用方法 updateFile(),但你可以很好地参数化如下: - $ /

  def updateFile(file,patternToFind,patternToReplace){
replacePatternInFile(file){
it.replaceAll(patternToFind,patternToReplace)
}
}


I have a file that I need to modify. The part I need to modify (not the entire file), is similar to the properties shown below. The problem is that I only need to replace part of the "value", the "ConfigurablePart" if you will. I receive this file so can not control it's format.

alpha.beta.gamma.1 = constantPart1ConfigurablePart1
alpha.beta.gamma.2 = constantPart2ConfigurablePart2
alpha.beta.gamma.3 = constantPart3ConfigurablePart3

I made this work this way, though I know it is really bad!

def updateFile(String pattern, String updatedValue) {
    def myFile = new File(".", "inputs/fileInherited.txt")
    StringBuffer updatedFileText = new StringBuffer()
    def ls = System.getProperty('line.separator')
    myFile.eachLine{ line ->
        def regex = Pattern.compile(/$pattern/)
        def m = (line =~ regex)
        if (m.matches()) {
            def buf = new StringBuffer(line)
            buf.replace(m.start(1), m.end(1), updatedValue)
            line = buf.toString()
        }
        println line
        updatedFileText.append(line).append(ls)
    }
    myFile.write(updatedFileText.toString())
}

The passed in pattern is required to contain a group that is substituted in the StringBuffer. Does anyone know how this should really be done in Groovy?

EDIT -- to define the expected output

The file that contains the example lines needs to be updated such that the "ConfigurablePart" of each line is replaced with the updated text provided. For my ugly solution, I would need to call the method 3 times, once to replace ConfigurablePart1, once for ConfigurablePart2, and finally for ConfigurablePart3. There is likely a better approach to this too!!!

*UPDATED -- Answer that did what I really needed *

In case others ever hit a similar issue, the groovy code improvements I asked about are best reflected in the accepted answer. However, for my problem that did not quite solve my issues. As I needed to substitute only a portion of the matched lines, I needed to use back-references and groups. The only way I could make this work was to define a three-part regEx like:

(.*)(matchThisPart)(.*)

Once that was done, I was able to use:

it.replaceAdd(~/$pattern/, "\$1$replacement\$3")

Thanks to both replies - each helped me out a lot!

解决方案

It can be made more verbose with the use of closure as args. Here is how this can be done:

//abc.txt
abc.item.1 = someDummyItem1
abc.item.2 = someDummyItem2
abc.item.3 = someDummyItem3
alpha.beta.gamma.1 = constantPart1ConfigurablePart1
alpha.beta.gamma.2 = constantPart2ConfigurablePart2
alpha.beta.gamma.3 = constantPart3ConfigurablePart3
abc.item.4 = someDummyItem4
abc.item.5 = someDummyItem5
abc.item.6 = someDummyItem6

Groovy Code:-

//Replace the pattern in file and write to file sequentially.
def replacePatternInFile(file, Closure replaceText) {
    file.write(replaceText(file.text))
}

def file = new File('abc.txt')
def patternToFind = ~/ConfigurablePart/
def patternToReplace = 'NewItem'

//Call the method
replacePatternInFile(file){
    it.replaceAll(patternToFind, patternToReplace)
}

println file.getText()

//Prints:
abc.item.1 = someDummyItem1
abc.item.2 = someDummyItem2
abc.item.3 = someDummyItem3
alpha.beta.gamma.1 = constantPart1NewItem1
alpha.beta.gamma.2 = constantPart2NewItem2
alpha.beta.gamma.3 = constantPart3NewItem3
abc.item.4 = someDummyItem4
abc.item.5 = someDummyItem5
abc.item.6 = someDummyItem6

Confirm file abc.txt. I have not used the method updateFile() as done by you, but you can very well parameterize as below:-

def updateFile(file, patternToFind, patternToReplace){
   replacePatternInFile(file){
      it.replaceAll(patternToFind, patternToReplace)
   }
} 

这篇关于需要Groovy方法来完成部分文件替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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