Unix groovy中是否有grep、pipe和cat的API?
groovy中是否有grep、pipe、cat的API?我不确定是否理解您的问题 你的意思是进行系统调用并通过管道传递结果吗 如果是这样,您可以执行以下操作:Unix groovy中是否有grep、pipe和cat的API?,unix,groovy,Unix,Groovy,groovy中是否有grep、pipe、cat的API?我不确定是否理解您的问题 你的意思是进行系统调用并通过管道传递结果吗 如果是这样,您可以执行以下操作: println 'cat /Users/tim_yates/.bash_profile'.execute().text 打印文件内容的步骤 您还可以管道化进程输出: def proc = 'cat /Users/tim_yates/.bash_profile'.execute() | 'grep git'.execute() print
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调用获取文件的文本,可以执行以下操作:
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
}
这正是我想要的:)显然,如果文件很大,你会希望使用File.eachLine
逐行扫描它们,而不是上面的File.text.tokenize
代码,它会将整个文件加载到RAM中。祝你好运,玩得开心!关于我使用它的原因的博客:我建议您将.tokenize('\n')替换为.readLines()。这样会更干净,更独立于系统。好主意。这个方法直到6个月后才出现在groovy中