Is there an API for grep, pipe, cat in groovy?
[:] creates an empty Map. The colon is there to distinguish it from [] , which creates an empty List. This groovy code: def foo = [:]
The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.
There is no need for semi-colon ; at the end of the statement. Alternatively you can use the println function that will automatically append a newline to the end of the output. println "Hello World!"
Not sure I understand your question.
Do you mean make system calls and pipe the results?
If so, you can just do something like:
println 'cat /Users/tim_yates/.bash_profile'.execute().text
To print the contents of a file
You can pipe process output as well:
def proc = 'cat /Users/tim_yates/.bash_profile'.execute() | 'grep git'.execute()
println proc.text
If you want to get the text of a File
using standard Groovy API calls, you can do:
println new File( '/Users/tim_yates/.bash_profile' ).text
And this gets a list of the lines in a file, finds all that contain the word git
then prints each one out in turn:
new File( '/Users/tim_yates/.bash_profile' ).text.tokenize( '\n' ).findAll {
it.contains 'git'
}.each {
println it
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With