# Add lines of text to a file

**URL:** <https://discourse.nodered.org/t/add-lines-of-text-to-a-file/77745>\
**Category:** General\
**Tags:** function-node\
**Created:** [20 April 2023 14:27 UTC](https://discourse.nodered.org/t/add-lines-of-text-to-a-file/77745 "2023-04-20T14:27:13Z")\
**Posts on this page:** 1\
**Showing post:** 10

<div class="post-metadata">

**Author:** ![SuperNinja](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/superninja/32/5761_2.png) [@SuperNinja](https://discourse.nodered.org/u/SuperNinja)\
**Post date:** [26 April 2023 08:26 UTC](https://discourse.nodered.org/t/add-lines-of-text-to-a-file/77745/10 "2023-04-26T08:26:22Z")

</div>

Great work! I learn a lot with your code. THANK YOU  
And yes there are pitfalls that come out little by little 😄... here's another one when I thought I was done with everything : I would like to display the comment lines.

Do you have an idea ?

... and if it tells you... While waiting for your answer I noticed a `\n` in the file I want to modify:  
`static const char lg_clic1sec_cancel_en[] = {"Click 1sec:\nCancel"};`.  
As a result, the lines are cut and the final text incomplete::  
`static const char lg_clic1sec_cancel_en[] = {"Click 1sec"};`

> [@How to tell the difference between \\n in a string and a newline?](https://discourse.nodered.org/t/how-to-tell-the-difference-between-n-in-a-string-and-a-newline/77922/9):
>
> Try let lines = file.split(/(?\<!:)\n/) But it would be best to correct the code and remove non valid property characters from your property names, then you would not have to patch the issue. Just for info const foo = file.replace( ":\n", ":::") will only replace first occurance. it should be const foo = file.replace( /:\n/g, ":::") for multiple replaces.

**[EDIT]** line breaks have been resolved and included in the code below.

We are nearing the end ! Here is your last modified code to add comment lines to the newFile:

```auto
let file = `
/*
 * language.c
 *
 * Created: 01/02/2022 15:15:09
 * Author: 
 */ 

#include <asf.h>

#include "language.h"

//Divers
static const char lg_ok_fr[] = {"Ok"};
static const char lg_ok_en[] = {"Ok"};
const char * lg_ok[] = {lg_ok_fr, lg_ok_en};

static const char lg_no_fr[] = {"Non"};
static const char lg_no_en[] = {"No"};
const char * lg_no[] = {lg_no_fr, lg_no_en};

static const char lg_yes_fr[] = {"Oui"};
static const char lg_yes_en[] = {"Yes"};
const char * lg_yes[] = {lg_yes_fr, lg_yes_en};

//Annule 
static const char lg_clic1sec_cancel_fr[] = {"Clic 1sec:\nAnnule"};
static const char lg_clic1sec_cancel_en[] = {"Click 1sec:\nCancel"};
const char * lg_clic1sec_cancel[] = {lg_clic1sec_cancel_fr, lg_clic1sec_cancel_en};	

`
//replace & split pour ne pas avoir de saut de ligne dans le resultat
file = file.replace(/:\n/g, ":\\n");//remplace les :\n par des :\\n
//node.warn(file)
let lines = file.split(/(?<!\\)\n/)//sépare ligne par ligne avec les \n en saut de ligne
//node.warn(lines)//affiche lignes par ligne originales

node.warn(lines)

let result = {}
let languageData = {}
for (let i = 0; i < lines.length; i++) {
    if (lines[i].startsWith('static const char')) {
        let translationStart = (lines[i].indexOf('{')) + 1
        let translationEnd = (lines[i].indexOf('}'))
        let translation = lines[i].slice(translationStart, translationEnd).replaceAll('"', '').trim()

        let dataStart = (lines[i].indexOf('lg_')) + 3
        let dataEnd = (lines[i].indexOf('['))
        let data = lines[i].slice(dataStart, dataEnd)

        let parts = data.split('_')

        let language = parts.pop()
        let word = parts.join('_')
 
        languageData = { [word]: translation }

        result[language] = { ...result[language], ...languageData }

    }

}

let deTranslation = ['Ok', 'Nicht', 'Ja']
let words = Object.keys(result.en)
let data = {}
for (let i = 0; i < deTranslation.length; i++) {
    data = { ...data, ...{[words[i]]: deTranslation[i] }}
    result = { ...result, ...{'de': data}}
}

let newFile = ''
let languages = Object.keys(result)
for (let i = 0; i < words.length; i++) {
    for (let j = 0; j < languages.length; j++) {
        newFile = newFile + '\n' + `static const char lg_${words[i]}_${languages[j]}[] = {"${result[languages[j]][words[i]]}"};`
    }
    newFile = newFile + '\n' + `const char * lg_${ words[i] } [] = {`

    let addition = ''
    for (let j = 0; j < languages.length - 1; j++) {
        addition = addition + `lg_${words[i]}_${languages[j]},`
    }
    newFile = newFile + addition + `lg_${words[i]}_${languages[languages.length - 1]}};`
    newFile = newFile + '\n'
}
//if replace (at first)
//newFile = file.replace( /:::/g, ":\n")

//node.warn(newFile)
msg.payload = newFile
return msg

```

---

_[View the full topic](https://discourse.nodered.org/t/add-lines-of-text-to-a-file/77745)._
