2011-03-01 23 views

回答

11

不知道我理解你的問題。

你的意思是進行系統調用和管道結果?

如果是這樣,你可以這樣做:

println 'cat /Users/tim_yates/.bash_profile'.execute().text 

要打印文件

可以通過管道輸出過程的內容,以及:

def proc = 'cat /Users/tim_yates/.bash_profile'.execute() | 'grep git'.execute() 
println proc.text 

如果你想要使用標準的Groovy API調用來獲取File的文本,您可以執行:

println new File('/Users/tim_yates/.bash_profile').text 

這得到一個文件中的行的列表,找到所有包含單詞git然後打印每一個出依次爲:

new File('/Users/tim_yates/.bash_profile').text.tokenize('\n').findAll { 
    it.contains 'git' 
}.each { 
    println it 
} 
+0

這正是我想要的:) – 2011-03-01 12:07:55

+0

顯然,如果文件非常龐大,您需要使用File.eachLine而不是上面的File.text.tokenize代碼逐行掃描它們,這會將整個文件加載到RAM中。祝好運並玩得開心點! – 2011-03-01 12:17:12

+0

關於我使用它的博客:http://schulstad.blogspot.com/2011/03/logs-use-groovygrep-to-find-trends-in.html – 2011-03-02 18:02:07