Statistics
| Revision:

root / tmp / org.txm.groovy.core / src / groovy / org / txm / scripts / swb / Grep.groovy @ 1000

History | View | Annotate | Download (2.8 kB)

1
package org.txm.scripts.scripts
2
// Copyright © - ENS de Lyon - http://textometrie.ens-lyon.fr
3
//
4
// This file is part of the TXM platform.
5
//
6
// The TXM platform is free software: you can redistribute it and/or modify
7
// it under the terms of the GNU General Public License as published by
8
// the Free Software Foundation, either version 3 of the License, or
9
// (at your option) any later version.
10
//
11
// The TXM platform is distributed in the hope that it will be useful,
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
// GNU General Public License for more details.
15
//
16
// You should have received a copy of the GNU General Public License
17
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
18
//
19
// $LastChangedDate: 2012-02-01 17:58:45 +0100 (mer., 01 févr. 2012) $
20
// $LastChangedRevision: 2122 $
21
// $LastChangedBy: mdecorde $
22
//
23

    
24
// PARAMETERS //
25
outfile = new File("/home/mdecorde/xml/test/grep")
26
dir = new File("/home/mdecorde/xml/test/")                        // **change this parameter**
27
ext = "\\.xml"                                      // **change this parameter**
28
find = /(<...+)/                                                                   // **change this parameter**
29
replaceWith = 'gp1=$1'                                             // **change this parameter**
30

    
31
// MAIN //
32

    
33
grep(dir, outfile, ext, find, replaceWith) // process a directory
34
//grep(dir, outfile, null, find, replaceWith) // process a directory with no constraint on files
35
//grep(dir, outfile, ext, find, null) // directory + don't replace
36

    
37
//grep(infile, outfile, find, replaceWith) // process a file
38
//grep(dir, outfile, find, null) // file + don't replace
39

    
40
// PUBLIC METHODS //
41

    
42
// to process a directory
43
def grep(File dir, File outfile, def ext, def regex, def replace) {
44
        if (dir.isDirectory()) {
45
                outfile.createNewFile()
46
                if (ext == null) ext = /.+/
47
                outfile.write('')                            // create empty file
48
                outfile.withWriter { writer ->
49
                        dir.eachFileMatch(~/.*$ext/){ file ->    // for each file matching extension
50
                                _grep(file, writer, regex, replace)
51
                        }
52
                }
53
                return true;
54
        } else {
55
                println "$outfile is not a directory"
56
                return false;
57
        }
58
}
59

    
60
// to process a file with outfile
61
def grep(File file, File outfile, def regex, def replace) {
62
        if (file.isDirectory()) return false;
63
        outfile.withWriter { writer ->
64
                _grep(file, writer, regex, replace)
65
        }
66
        return true;
67
}
68

    
69
// PRIVATE METHODS //
70

    
71
// to process a file with writer
72
private def _grep(def file, def writer, def regex, def replace) {
73
        file.eachLine { line ->
74
                _testLine(line, writer, regex, replace)
75
        }
76
}
77

    
78
private def _testLine(String line, def writer, def regex, def replace) {
79
        if (replace == null) {
80
                if (line.matches(find))
81
                        writer.println line
82
        } else {
83
                if (line.matches(find))
84
                        writer.println line.replaceAll(regex, replace)
85
        }        
86
}