在groovy中是否有grep,pipe,cat的API?
答案 0 :(得分:16)
不确定我理解你的问题。
您的意思是进行系统调用并管道结果吗?
如果是这样,您可以执行以下操作:
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
}