Skip to content

Instantly share code, notes, and snippets.

@nobeans
Created June 3, 2011 07:54
Show Gist options
  • Save nobeans/1006025 to your computer and use it in GitHub Desktop.
Save nobeans/1006025 to your computer and use it in GitHub Desktop.
final COUNT_TO_WRITE = 5
def lineNo = 1
def writableCount = 0
System.in.eachLine {
if (it =~ /hoge/) {
writableCount = COUNT_TO_WRITE
}
if (writableCount > 0) {
println "$lineNo: $it"
writableCount--
}
lineNo++
}
@nobeans
Copy link
Author

nobeans commented Jun 3, 2011

Alternative implementation of "grep -A".

$ cat test.txt | groovy grtail.groovy
15: xhoge
16: x1
17: x2
18: x3
19: x4
30: yhoge
31: y1
32: y2
33: y3
34: y4
45: zhoge
46: z1
47: z2
48: z3
49: z4

I recommend to use the groovyclient of GroovyServ instead of groovy!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment