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 1000 mdecorde
package org.txm.scripts.scripts
2 321 mdecorde
// Copyright © - ENS de Lyon - http://textometrie.ens-lyon.fr
3 321 mdecorde
//
4 321 mdecorde
// This file is part of the TXM platform.
5 321 mdecorde
//
6 321 mdecorde
// The TXM platform is free software: you can redistribute it and/or modify
7 321 mdecorde
// it under the terms of the GNU General Public License as published by
8 321 mdecorde
// the Free Software Foundation, either version 3 of the License, or
9 321 mdecorde
// (at your option) any later version.
10 321 mdecorde
//
11 321 mdecorde
// The TXM platform is distributed in the hope that it will be useful,
12 321 mdecorde
// but WITHOUT ANY WARRANTY; without even the implied warranty of
13 321 mdecorde
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 321 mdecorde
// GNU General Public License for more details.
15 321 mdecorde
//
16 321 mdecorde
// You should have received a copy of the GNU General Public License
17 321 mdecorde
// along with the TXM platform.  If not, see <http://www.gnu.org/licenses/>.
18 321 mdecorde
//
19 321 mdecorde
// $LastChangedDate: 2012-02-01 17:58:45 +0100 (mer., 01 févr. 2012) $
20 321 mdecorde
// $LastChangedRevision: 2122 $
21 321 mdecorde
// $LastChangedBy: mdecorde $
22 321 mdecorde
//
23 321 mdecorde
24 321 mdecorde
// PARAMETERS //
25 321 mdecorde
outfile = new File("/home/mdecorde/xml/test/grep")
26 321 mdecorde
dir = new File("/home/mdecorde/xml/test/")                        // **change this parameter**
27 321 mdecorde
ext = "\\.xml"                                      // **change this parameter**
28 321 mdecorde
find = /(<...+)/                                                                   // **change this parameter**
29 321 mdecorde
replaceWith = 'gp1=$1'                                             // **change this parameter**
30 321 mdecorde
31 321 mdecorde
// MAIN //
32 321 mdecorde
33 321 mdecorde
grep(dir, outfile, ext, find, replaceWith) // process a directory
34 321 mdecorde
//grep(dir, outfile, null, find, replaceWith) // process a directory with no constraint on files
35 321 mdecorde
//grep(dir, outfile, ext, find, null) // directory + don't replace
36 321 mdecorde
37 321 mdecorde
//grep(infile, outfile, find, replaceWith) // process a file
38 321 mdecorde
//grep(dir, outfile, find, null) // file + don't replace
39 321 mdecorde
40 321 mdecorde
// PUBLIC METHODS //
41 321 mdecorde
42 321 mdecorde
// to process a directory
43 321 mdecorde
def grep(File dir, File outfile, def ext, def regex, def replace) {
44 321 mdecorde
        if (dir.isDirectory()) {
45 321 mdecorde
                outfile.createNewFile()
46 321 mdecorde
                if (ext == null) ext = /.+/
47 321 mdecorde
                outfile.write('')                            // create empty file
48 321 mdecorde
                outfile.withWriter { writer ->
49 321 mdecorde
                        dir.eachFileMatch(~/.*$ext/){ file ->    // for each file matching extension
50 321 mdecorde
                                _grep(file, writer, regex, replace)
51 321 mdecorde
                        }
52 321 mdecorde
                }
53 321 mdecorde
                return true;
54 321 mdecorde
        } else {
55 321 mdecorde
                println "$outfile is not a directory"
56 321 mdecorde
                return false;
57 321 mdecorde
        }
58 321 mdecorde
}
59 321 mdecorde
60 321 mdecorde
// to process a file with outfile
61 321 mdecorde
def grep(File file, File outfile, def regex, def replace) {
62 321 mdecorde
        if (file.isDirectory()) return false;
63 321 mdecorde
        outfile.withWriter { writer ->
64 321 mdecorde
                _grep(file, writer, regex, replace)
65 321 mdecorde
        }
66 321 mdecorde
        return true;
67 321 mdecorde
}
68 321 mdecorde
69 321 mdecorde
// PRIVATE METHODS //
70 321 mdecorde
71 321 mdecorde
// to process a file with writer
72 321 mdecorde
private def _grep(def file, def writer, def regex, def replace) {
73 321 mdecorde
        file.eachLine { line ->
74 321 mdecorde
                _testLine(line, writer, regex, replace)
75 321 mdecorde
        }
76 321 mdecorde
}
77 321 mdecorde
78 321 mdecorde
private def _testLine(String line, def writer, def regex, def replace) {
79 321 mdecorde
        if (replace == null) {
80 321 mdecorde
                if (line.matches(find))
81 321 mdecorde
                        writer.println line
82 321 mdecorde
        } else {
83 321 mdecorde
                if (line.matches(find))
84 321 mdecorde
                        writer.println line.replaceAll(regex, replace)
85 321 mdecorde
        }
86 321 mdecorde
}