Skip to content

Instantly share code, notes, and snippets.

@willis7
Forked from tamirko/replaceInFile.groovy
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save willis7/33c478e332de7d9e8fc3 to your computer and use it in GitHub Desktop.

Select an option

Save willis7/33c478e332de7d9e8fc3 to your computer and use it in GitHub Desktop.

Revisions

  1. willis7 revised this gist Sep 13, 2014. 1 changed file with 1 addition and 3 deletions.
    4 changes: 1 addition & 3 deletions replaceInFile.groovy
    Original file line number Diff line number Diff line change
    @@ -9,9 +9,7 @@

    def myFile = new File("data.txt")
    def fileText = myFile.text
    for ( index in 1..9 ) {
    fileText = (fileText =~ /James/).replaceFirst("user${index}")
    }
    ( 1..9 ).each { fileText = (fileText =~ /James/).replaceFirst("user${it}") }
    myFile.write(fileText)

    /* Usage in Cloudify:
  2. Tamir Korem revised this gist Aug 5, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions replaceInFile.groovy
    Original file line number Diff line number Diff line change
    @@ -19,6 +19,7 @@ myFile.write(fileText)
    with the current actual http port and all the occurrences of 8009
    with the current actual AJP port.
    */

    def config = new ConfigSlurper().parse(new File("tomcat-service.properties").toURL())
    def context = ServiceContextFactory.getServiceContext()
    def home = "${context.serviceDirectory}/${config.name}"
  3. Tamir Korem revised this gist Aug 5, 2012. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions replaceInFile.groovy
    Original file line number Diff line number Diff line change
    @@ -15,9 +15,9 @@ for ( index in 1..9 ) {
    myFile.write(fileText)

    /* Usage in Cloudify:
    The following code snippet replaces all the occurrences of 8080 with the current actual http port.
    and all the occurrences of 8009 with the current actual AJP port.
    The following code snippet replaces all the occurrences of 8080
    with the current actual http port and all the occurrences of 8009
    with the current actual AJP port.
    */
    def config = new ConfigSlurper().parse(new File("tomcat-service.properties").toURL())
    def context = ServiceContextFactory.getServiceContext()
  4. Tamir Korem revised this gist Aug 5, 2012. 1 changed file with 18 additions and 1 deletion.
    19 changes: 18 additions & 1 deletion replaceInFile.groovy
    Original file line number Diff line number Diff line change
    @@ -12,4 +12,21 @@ def fileText = myFile.text
    for ( index in 1..9 ) {
    fileText = (fileText =~ /James/).replaceFirst("user${index}")
    }
    myFile.write(fileText)
    myFile.write(fileText)

    /* Usage in Cloudify:
    The following code snippet replaces all the occurrences of 8080 with the current actual http port.
    and all the occurrences of 8009 with the current actual AJP port.
    */
    def config = new ConfigSlurper().parse(new File("tomcat-service.properties").toURL())
    def context = ServiceContextFactory.getServiceContext()
    def home = "${context.serviceDirectory}/${config.name}"
    portIncrement = 0
    def serverXmlFile = new File("${home}/conf/server.xml")
    def serverXmlText = serverXmlFile.text
    portReplacementStr = "port=\"${config.port + portIncrement}\""
    ajpPortReplacementStr = "port=\"${config.ajpPort + portIncrement}\""
    serverXmlText = serverXmlText.replace("port=\"8080\"", portReplacementStr)
    serverXmlText = serverXmlText.replace("port=\"8009\"", ajpPortReplacementStr)
    serverXmlFile.write(serverXmlText)
  5. Tamir Korem created this gist Aug 5, 2012.
    15 changes: 15 additions & 0 deletions replaceInFile.groovy
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,15 @@
    /* Groovy Usage:
    The following example replaces:
    the 1st occurrence of "James" in data.txt with "user1",
    the 2nd occurrence of "James" in data.txt with "user2",
    the 3rd occurrence of "James" in data.txt with "user3",
    ..
    the 9th occurrence of "James" in data.txt with "user9".
    */

    def myFile = new File("data.txt")
    def fileText = myFile.text
    for ( index in 1..9 ) {
    fileText = (fileText =~ /James/).replaceFirst("user${index}")
    }
    myFile.write(fileText)