Replace values in a text file without rewriting the whole text file

Hi everyone,

I have a text file below that I use to parameterize numerical simulations. I’d like to be able to change the values in the red box below from the data I put in an Excel file.
image

However, I’m encountering two problems :
1st problem: Transforming “V_xmin” into the value “0” is fairly straightforward using the “String Replace” module, but it requires the template to be used each time a value is changed. The line starts each time with xmin. Do you know if it’s possible to delete the entire line content and replace it with “xmin 0”?

2nd problem: Once I’ve modified the values, when I want to rewrite the file, I use the CSV Writer module, but for lines 27+, a certain organization of the lines is required (with precise tabulations), which is not respected. In reality, only lines 17-25 need to be modified. Is it possible to rewrite only these lines?
image
image

Thank you for your help.
blockMeshDict.txt (1.4 KB)

This could probably be something for which you can use the new Handlebars templating node in plain “Text” mode. Put the template and add tags for the variable parts, generate the string, and write it out to a file:

You can even do this for multiple rows, respectively files, in one go.

1 Like

Hi, thank you for your proposition, unfortunatly I met some difficulties with these two new nodes as there are no exemples associated. Here is my workflow :

The JSON conversion of my text isn’t really working due to some caracters (such as ||), so I end up with this :

With the handlebars, I end up with a result column with all the same values and when I save this as files with the StringCell To File node, I end up with multiple files with the name of the row as file name :
image

I think I misunderstood something on the process. Here is my workflow (KNIME_populate_temp – KNIME Community Hub), could you give me some advice ?
blockMeshDict_Template_pop.txt (63 Bytes)
blockMeshDict.txt (1.4 KB)

Hello,

thanks for giving it a try and for providing your current workflow. You’ll find my working modifications below. I realize that there’s a good example workflow missing at the moment - this is on me.

To the explanation:

  1. You’ll need to put the template with the placeholders in the the node’s configuration “Template” field.

  2. Variables to be used in the template can be passed via Flow Variable and / or via JSON column.

  3. To access a variable value called e.g. xmin from the JSON data in the template, use e.g. {{ data.xmin }} (in case it is supplied via flow variable, use {{ variables.flowVariableName }}

  4. (advanced) You can use Handlebars syntax, such as loops, conditions, string helpers, etc.

The updated workflow is available on my public NodePit Space:

Hope that helps!

Best regards,
Philipp

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.