Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an API for grep, pipe, cat in groovy?

Tags:

unix

groovy

Is there an API for grep, pipe, cat in groovy?

like image 210
Espen Schulstad Avatar asked Mar 01 '11 11:03

Espen Schulstad


People also ask

What does [:] mean in groovy?

[:] creates an empty Map. The colon is there to distinguish it from [] , which creates an empty List. This groovy code: def foo = [:]

How do I use grep to search?

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'.

How do I print a line in groovy?

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!"


1 Answers

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
}
like image 136
tim_yates Avatar answered Oct 21 '22 04:10

tim_yates