tags:

views:

78

answers:

1

I have a sample file like the following:

CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11
(
asdfa
)
CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11
(
asdfas
)

some other text in file

I want to convert this file into following:

CREATE GLOBAL TEMPORARY TABLE 1
(
asdfa
)
CREATE GLOBAL TEMPORARY TABLE 2
(
asdfas
)

some other text in file

So basically every TEMPORARY TABLE occurrence will have number appended to it.

So far I have the following groovy script:

int i = 0
new File ("C:\\Not_Modified").eachFile{file ->
        println "File name: ${file.name}"
        new File ("C:\\Not_Modified\\"+file.name).eachLine {line ->
                if (line.indexOf("TEMPORARY TABLE")>0)
                {
                    i++
                }
            }
        println "There are ${i} occurences of TEMPORARY TABLE"
    }

How can I change the text in the file? should I be writing to a different file?

btw, I have directory in my script because I will be working on lot of these type of files in a directory.

I should have opted perl for this task but wanted to give groovy a try.

+1  A: 

I'm think you should write to different file, it's a good practice. Put something like line below inside your if {} (instead of i++)

line = line.replaceFirst(/^(create temporary table) (.*)/, "\$1 table${++i}")

and then, outside of your if write line variable into outfile

BTW i'm think you better use ==~ in your if instead of indexOf

Alexey Sviridov