Groovy replacing place holders of file content similar to multiline-string

Rao

Got the following script which replaces values in the multi line string.

def param1 = 'Groovy'
def param2 = 'Java'
def multiline = """
${param1} is closely related to ${param2},
so it is quite easy to make a transition.
"""
//output shows with the replaced values for param1 and param2
println multiline

Output is shown as expected:

Groovy is closely related to Java,
so it is quite easy to make a transition.

Issue:

Now I am trying to do the same using file instead of multi line string. i.e., copied the multi line string to a file and using the below script to do the same but not working(not giving the desired result).

I am sure, it must be something I am missing. Tried multiple way, but went futile.

Try #1: Script

def param1 = 'Groovy'
def param2 = 'Java'
def multiline = Eval.me(new File('test.txt').text)
println multiline

And it fails to run. Error follows:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed: Script1.groovy: 1: expecting EOF, found ',' @ line 1, column 42. s closely related to ${param2}, ^
1 error

Try #2

def param1 = 'Groovy'
def param2 = 'Java'
def multiline = new File('test.txt').text
def finalContent = """$multiline"""
println finalContent

And there is no difference in the output, just showing the file content as it is.

Output:

${param1} is closely related to ${param2},
so it is quite easy to make a transition.

Any pointers what am I missing?

Please note that at the moment I want to avoid file content modification using replace() method.

Opal

Not sure why it doesn't work, however what I may suggest here is that templating suits best here. Please have a look:

import groovy.text.SimpleTemplateEngine

def f = new File('lol.txt')
println f.text

def binding = [
    param1: 'Groovy',
    param2: 'Java',
]

def engine = new SimpleTemplateEngine()
def template = engine.createTemplate(f.text).make(binding)

println template.toString()

An explanation why file content is not evaluated may be found here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Groovy replacing place holders of file content similar to multiline-string

From Dev

Replacing place holders with letter in Hangman C++

From Dev

Replace string with multiline file content

From Dev

Replace string with multiline file content

From Dev

Replace a multiline string with the content of a file.txt

From Dev

Place holders in Yesod subsite

From Dev

Indent multiline string with Groovy onliner

From Dev

Groovy multiline string keep new line and indentation

From Dev

Is there a way to use place holders in Apache PropertiesConfiguration

From Dev

Select statement as column value with place holders

From Dev

How to Resolve Custom Place Holders in Email Template

From Dev

Replacing string in file with perl

From Dev

Replacing string in a file with script

From Dev

sed not replacing string in file

From Dev

Groovy regex when replacing tokens in file

From Dev

Replacing multiline batch file with a for loop which reads a text file

From Dev

Echo multiline string into file bash

From Dev

Search for multiline String in a text file

From Dev

Replace text in file with multiline string

From Dev

Capturing multiline string with regex and replacing it with itself, indented on every line

From Dev

Parsing and replacing values in an irregular multiline text file with powershell

From Dev

Replacing {{string}} within php file

From Dev

Replacing a string in nth line a file

From Dev

Replacing a string by a file using Sed

From Dev

Searching text string in file & replacing it

From Dev

Replacing string with line number in a file

From Dev

Replacing the string content in a List of an object in Java

From Dev

Replacing the string content in a List of an object in Java

From Dev

groovy read a file, resolve variables in file content

Related Related

  1. 1

    Groovy replacing place holders of file content similar to multiline-string

  2. 2

    Replacing place holders with letter in Hangman C++

  3. 3

    Replace string with multiline file content

  4. 4

    Replace string with multiline file content

  5. 5

    Replace a multiline string with the content of a file.txt

  6. 6

    Place holders in Yesod subsite

  7. 7

    Indent multiline string with Groovy onliner

  8. 8

    Groovy multiline string keep new line and indentation

  9. 9

    Is there a way to use place holders in Apache PropertiesConfiguration

  10. 10

    Select statement as column value with place holders

  11. 11

    How to Resolve Custom Place Holders in Email Template

  12. 12

    Replacing string in file with perl

  13. 13

    Replacing string in a file with script

  14. 14

    sed not replacing string in file

  15. 15

    Groovy regex when replacing tokens in file

  16. 16

    Replacing multiline batch file with a for loop which reads a text file

  17. 17

    Echo multiline string into file bash

  18. 18

    Search for multiline String in a text file

  19. 19

    Replace text in file with multiline string

  20. 20

    Capturing multiline string with regex and replacing it with itself, indented on every line

  21. 21

    Parsing and replacing values in an irregular multiline text file with powershell

  22. 22

    Replacing {{string}} within php file

  23. 23

    Replacing a string in nth line a file

  24. 24

    Replacing a string by a file using Sed

  25. 25

    Searching text string in file & replacing it

  26. 26

    Replacing string with line number in a file

  27. 27

    Replacing the string content in a List of an object in Java

  28. 28

    Replacing the string content in a List of an object in Java

  29. 29

    groovy read a file, resolve variables in file content

HotTag

Archive