簡體   English   中英

Groovy:在文件中獲取特定值

[英]Groovy: Get a specific value in a file

我在Groovy中很新。 基本上,我加載一個文本文件,然后需要在一行(實際上是第六行)上獲得一個特定的值。

這行就像:
STATIC_ASSERT(VERSION == 888888, "blablabla");

我需要獲得888888價值。

我找到了一種使用多重拆分的方法,但是很丑陋。

我也想到使用類似的東西:
line.substring(ind+"VERSION ==".length(), line.length()-10).trim();

但是“ blablabla”的長度可以改變。

謝謝。


編輯:它使用這樣的硬編碼字符串工作。

但是,當我嘗試從文件運行它時,出現此錯誤:

test' is failed: org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: 
script1516208488151762512206.groovy: 4: expecting '}', found '' @ line 4, column 69. 
ne.contains('VERSION ==') 
^ 

1 error 

這是我的代碼:

${groovy:
    String Ver
    def file = new File("C:\\test.cpp")
    def data = file.filterLine { line -> 
        line.contains('VERSION ==')
    }
    Ver = data.split("==")[1].split(",")[0].trim()

    logger.info(Ver)
}

-我也嘗試過這樣的事情:

${groovy:
    String Ver
    def file = new File("C:\\test.cpp")
        while ((line = file.readLine())!=null) 
        {
           int ind = line.indexOf("VERSION ==")
           if (ind >= 0)
           {
               Ver = line.split("==")[1].split(",")[0].trim()
           }
        }
        logger.info(Ver)
    }

但是我得到了同樣奇怪的錯誤:

expecting '}', found '' @ line 9, column 58. 
("==")[1].split(",")[0].trim() 
^ 

1 error 

:(

您可以如下所示進行:

def line = 'STATIC_ASSERT(VERSION == 888888, "blablabla");'
println line.split('==')[1].split(',')[0].trim()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM