Groovy:编辑文件

Groovy:编辑文件,groovy,Groovy,我有如下示例文件: CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11 ( asdfa ) CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11 ( asdfas ) some other text in file CREATE GLOBAL TEMPORARY TABLE 1 ( asdfa ) CREATE GLOBAL TEMPORARY TABLE 2 ( asdfas ) some other text

我有如下示例文件:

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

some other text in file
CREATE GLOBAL TEMPORARY TABLE 1
(
asdfa
)
CREATE GLOBAL TEMPORARY TABLE 2
(
asdfas
)

some other text in file
我想将此文件转换为以下内容:

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

some other text in file
CREATE GLOBAL TEMPORARY TABLE 1
(
asdfa
)
CREATE GLOBAL TEMPORARY TABLE 2
(
asdfas
)

some other text in file
所以,基本上每个临时表引用都会附加数字

到目前为止,我有以下groovy脚本:

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"
    }
如何更改文件中的文本?我应该写入另一个文件吗

顺便说一句,我的脚本中有目录,因为我将在一个目录中处理许多此类文件


我本应该选择perl来完成这项任务,但我想尝试一下groovy

我觉得你应该写不同的文件,这是一个很好的做法。 在if{}(而不是i++)中放一些类似于下面这行的东西

line=line.replaceFirst(/^(创建临时表)(.*)/,“\$1表${++i}”) 然后,在if之外,将line变量写入outfile


顺便说一句,我认为最好在if中使用==~而不是indexOf

我认为应该写入不同的文件,这是一个很好的做法。 在if{}(而不是i++)中放一些类似于下面这行的东西

line=line.replaceFirst(/^(创建临时表)(.*)/,“\$1表${++i}”) 然后,在if之外,将line变量写入outfile


顺便说一句,我认为最好在if中使用==~而不是indexOf

我编写了一个类似File.eachLine{}的小函数,但可以编辑

您可以这样使用它:

def n=1 modifyFile("filename"){ if(it.startsWith("CREATE GLOBAL TEMPORARY TABLE")) return "CREATE GLOBAL TEMPORARY TABLE " + n++ return it // Re-inserts unmodified line" } def n=1 修改文件(“文件名”){ if(it.startsWith(“创建全局临时表”)) 返回“创建全局临时表”+n++ 返回它//重新插入未修改的行“ } 这很容易编码——从闭包返回的任何内容都会写入新文件

/** * This will completely re-write a file, be careful. * * Simple Usage: * * modifyFile("C:\whatever\whatever.txt") { * if(it.contains("soil")) * return null // remove dirty word * else * return it * } * * The closure must return the line passed in to keep it in the file or alter it, any alteration * will be written in it's place. * * To delete an entire line instead of changing it, return null * To add more lines after a given line return: it + "\n" + moreLines * * Notice that you add "\n" before your additional lines and not after the last * one because this method will normally add one for you. */ def modifyFile(srcFile, Closure c) { modifyFile(srcFile, srcFile, c) } def modifyFile(srcFile, destFile, Closure c={println it;return it}) { StringBuffer ret=new StringBuffer(); File src=new File(srcFile) File dest=new File(destFile) src.withReader{reader-> reader.eachLine{ def line=c(it) if(line != null) { ret.append(line) ret.append("\n") } } } dest.delete() dest.write(ret.toString()) } } /** *这将完全重新写入文件,请小心。 * *简单用法: * *修改文件(“C:\whater\whater.txt”){ *如果(它包含(“土壤”)) *返回空//删除脏字 *否则 *还它 * } * *闭包必须返回传入的行,以将其保留在文件中,或对其进行任何更改 *将被写在它的地方。 * *要删除整行而不是更改它,请返回null *要在给定行返回后添加更多行,请执行以下操作:it+“\n”+更多行 * *请注意,在附加行之前添加“\n”,而不是在最后一行之后 *一个,因为此方法通常会为您添加一个。 */ def修改文件(srcFile,闭包c){ 修改文件(srcFile,srcFile,c) } def modifyFile(srcFile、destFile、Closure c={println-it;return-it}){ StringBuffer ret=新的StringBuffer(); 文件src=新文件(srcFile) File dest=新文件(destFile) src.withReader{reader-> reader.eachLine{ def管路=c(it) 如果(行!=null){ ret.append(行) ret.append(“\n”) } } } 删除目标() dest.write(ret.toString()) } }
我编写了一个类似File.eachLine{}的小函数,但可以编辑

您可以这样使用它:

def n=1 modifyFile("filename"){ if(it.startsWith("CREATE GLOBAL TEMPORARY TABLE")) return "CREATE GLOBAL TEMPORARY TABLE " + n++ return it // Re-inserts unmodified line" } def n=1 修改文件(“文件名”){ if(it.startsWith(“创建全局临时表”)) 返回“创建全局临时表”+n++ 返回它//重新插入未修改的行“ } 这很容易编码——从闭包返回的任何内容都会写入新文件。如果需要不同的文件,请提供两个文件名

/** * This will completely re-write a file, be careful. * * Simple Usage: * * modifyFile("C:\whatever\whatever.txt") { * if(it.contains("soil")) * return null // remove dirty word * else * return it * } * * The closure must return the line passed in to keep it in the file or alter it, any alteration * will be written in it's place. * * To delete an entire line instead of changing it, return null * To add more lines after a given line return: it + "\n" + moreLines * * Notice that you add "\n" before your additional lines and not after the last * one because this method will normally add one for you. */ def modifyFile(srcFile, Closure c) { modifyFile(srcFile, srcFile, c) } def modifyFile(srcFile, destFile, Closure c={println it;return it}) { StringBuffer ret=new StringBuffer(); File src=new File(srcFile) File dest=new File(destFile) src.withReader{reader-> reader.eachLine{ def line=c(it) if(line != null) { ret.append(line) ret.append("\n") } } } dest.delete() dest.write(ret.toString()) } } /** *这将完全重新写入文件,请小心。 * *简单用法: * *修改文件(“C:\whater\whater.txt”){ *如果(它包含(“土壤”)) *返回空//删除脏字 *否则 *还它 * } * *闭包必须返回传入的行,以将其保留在文件中,或对其进行任何更改 *将被写在它的地方。 * *要删除整行而不是更改它,请返回null *要在给定行返回后添加更多行,请执行以下操作:it+“\n”+更多行 * *请注意,在附加行之前添加“\n”,而不是在最后一行之后 *一个,因为此方法通常会为您添加一个。 */ def修改文件(srcFile,闭包c){ 修改文件(srcFile,srcFile,c) } def modifyFile(srcFile、destFile、Closure c={println-it;return-it}){ StringBuffer ret=新的StringBuffer(); 文件src=新文件(srcFile) File dest=新文件(destFile) src.withReader{reader-> reader.eachLine{ def管路=c(it) 如果(行!=null){ ret.append(行) ret.append(“\n”) } } } 删除目标() dest.write(ret.toString()) } }
只是好奇-你为什么建议==~而不是indexOf?它更快吗?我建议使用regexp,因为在这种情况下,您可以更精确地检测适当的“临时表”。换句话说,为了避免检测数据中的临时表(f.e./TEMPORARY TABLE.*/(/左右)…现在您有两个问题…:)只是好奇-为什么建议使用==~而不是indexOf?它更快吗?我建议使用regexp,因为在这种情况下,您可以更精确地检测适当的“临时表”。换句话说,为了避免在数据中检测临时表(f.e./TEMPORARY TABLE.*/(/左右)…现在您有两个问题…)