eachLine
Matcher
Collection
Map
String, GString
Number, Character
others objects
groovy:000> line = "hello world" ===> groovy:000> m = line =~ '[aeiou]' ===> groovy:000> m[0] ===> groovy:000> m[1] ===> groovy:000> m.size() ===>
Explique este programa:
1 #!/usr/bin/env groovy 2 if (args.length == 0 ) return 3 testfile = new File(args[0]) 4 testfile.eachLine { line -> 5 line.find(/(?ix) href \s* = \s* "( [^"]* )"/) { 6 delta, link -> println "delta = '$delta' link='$link'" 7 } 8 }
readLines
de un objeto File
?
f.text.findAll
?
groovy:000> m = f.text.findAll('\\n\\n+').size() ===> groovy:000> m = f.text.findAll('\\n').size() ===> m = f.text.findAll('(?i)\\bCaptain\\b').size() ===>
public boolean canRead()
public boolean canWrite()
public boolean exists()
public boolean isDirectory()
public boolean isFile()
public boolean isHidden()
generaciondecodigos@nereida:~/Lgroovy/files$ cat -n cli.groovy 1 #!/usr/bin/env groovy 2 //http://groovy.codehaus.org/gapi/groovy/util/CliBuilder.html 3 import groovy.util.CliBuilder 4 5 def cli = new CliBuilder(usage:'cli.groovy') 6 7 cli.a(longOpt:'all', 'display all files') 8 cli.l(longOpt:'long', 'use a long listing format') 9 cli.t(longOpt:'time', 'sort by modification time') 10 cli.t(longOpt:'time', 'sort by modification time') 11 cli.h(longOpt: 'help', 'Show usage information') 12 13 def options = cli.parse(args) 14 15 println options 16 17 println options.arguments() 18 19 println "a = "+options.a 20 println "l = "+options.l 21 println "long = "+options.long 22 println "t = "+options.t 23 println "h = "+options.h 24 25 if (options.h) cli.usage()¿Cuales son las salidas de estas ejecuciones?
generaciondecodigos@nereida:~/Lgroovy/files$ ./cli.groovy -h generaciondecodigos@nereida:~/Lgroovy/files$ ./cli.groovy -l s*.groovy generaciondecodigos@nereida:~/Lgroovy/files$ ./cli.groovy --long s*.groovy generaciondecodigos@nereida:~/Lgroovy/files$ ./cli.groovy -lat s*.groovy
groovy:000> cli = new CliBuilder() ===> groovy:000> cli.v(longOpt: 'verbose', 'verbose mode') ===> null groovy:000> cli.D(longOpt: 'Debug', 'display debug info') ===> null groovy:000> cli.o(longOpt: 'output', args:1, 'use/specify output file') ===> null groovy:000> options = cli.parse(['-v', '-D', '-o mifichero']) ===> groovy.util.OptionAccessor@1952853d groovy:000> options.v ===> groovy:000> options.D ===> groovy:000> options.o ===> groovy:000> options.output ===> groovy:000> options.verbose ===>
with
?
10 def sample = new Sample() 11 sample.with { 12 username = 'fulanito' 13 email = 'email@host.com' 14 println speakUp() 15 addLabel 'Groovy' 16 addLabel 'Java' 17 addLabel 'Perl' 18 }
~/LGCbook$ grep -i ^groov /usr/share/dict/words | groovy -n -e 'println line.toUpperCase()'
~/LGCbook$ grep -i ^groov /usr/share/dict/words | groovy -p -e 'line.toUpperCase()'
~/LGCbook$ groovy -p -i .bak -e '(line =~ "hola").replaceAll("hola CocaCola")' gcexamples/*.html
~/Lgroovy/files$ find . -name 'r*.groovy' | xargs groovy -i .bak -pe '(line =~ "//.*").replaceAll("")'
~/Lgroovy/files$ groovy -pi .bak -e "count + ': ' + line" prueba
~/Lgroovy/files$ groovy -p -e "if (line =~ /groovy/) count + ': ' + line" *.groovy
~/Lgroovy/files$ groovy -pe "if (count <=3) count+':'+line" tail.groovy ls.groovy
lhp@nereida:~$ ps aux | groovy -a -ne "if (split[0] =~ 'lhp') println split[-1]"
lhp@nereida:~$ groovy -a':' -ne 'if (split[0] =~ /^lh/) println split[0]' /etc/passwd
~/Lgroovy/files$ groovy -a -pe "split[0].toInteger()+split[-2].toInteger()" accounts.txt
lhp@nereida:~$ groovy -l -e "println line.reverse()"
new File(dirName).eachFile() { file -> if (file.getName() =~ regexp) { println file.getName() } }
public void eachFileMatch(Object, Closure)
?
public void eachFileRecurse(Closure)
?
name = ManagementFactory.getRuntimeMXBean().getName(); pid = Integer.parseInt(name[0..name.indexOf("@")-1]) println "Stop with 'kill -9 $pid or CTRL-C'"
if (!(fileName =~ /\.log$/)) fileName += '.log'
logfile.createNewFile();
file = new RandomAccessFile( logfile, "r" )
file.seek( filePointer ) while( (line = file.readLine()) != null ) { println '##' + line } filePointer = file.filePointer
generaciondecodigos@nereida:~/src/groovy/processes$ cat -n retcode.groovy 1 #!/usr/bin/env groovy 2 sarg = args.join(' ') 3 def command = """ 4 ls $sarg 5 """// Create the String 6 def proc = command.execute() 7 proc.waitFor() 8 9 println "return code: ${proc.exitValue()}" 10 println "stderr: ${proc.err.text}" 11 println "stdout: ${proc.in.text}"¿Cual es la salida?
generaciondecodigos@nereida:~/src/groovy/processes$ ls -l 'a file'
~/Lgroovy/files$ ls -l 'a file' -rw-r--r-- 1 casianorodriguezleon staff 28 16 abr 06:10 a file ~/Lgroovy/files$ groovysh Groovy Shell (1.6.5, JVM: 1.6.0_17) Type 'help' or '\h' for help. ----------------------------------------------------------------------------------------------------------------------------------------------------- groovy:000> r = ['ls', 'a file'].execute() ===> groovy:000> r.waitFor() ===> groovy:000> r.exitValue() ===> groovy:000> r.err.text ===> groovy:000> r.in.text ===>
groovy:000> u = 'http://nereida.deioc.ull.es/~generaciondecodigos/gcexamples/node26.html'.toURL() ===> http://nereida.deioc.ull.es/~generaciondecodigos/gcexamples/node26.html groovy:000> u.eachLine { if (it =~ /(?i)href/)println it }
tokenize
y split
?
groovy:000> System.getProperty("java.version")
groovy:000> System.env.each { println it }
$GROOVY_HOME/conf/groovy-starter.conf
?
ant
?
Casiano Rodríguez León