# Change a value into a text file

**URL:** <https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268>\
**Category:** General\
**Created:** [7 January 2022 20:40 UTC](https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268 "2022-01-07T20:40:32Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [7 January 2022 20:40 UTC](https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268/1 "2022-01-07T20:40:32Z")

</div>

Good evening, I need some help. I want to change a value in this file.

```auto
$ cat /etc/raspiwifi/raspiwifi.conf
ssid_prefix=SmartPhotons
auto_config=1
auto_config_delay=300
ssl_enabled=0
server_port=80
wpa_enabled=0
wpa_key=0

```

I read it, then split it into an array.

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/c/2/c278c88ab3d5a19bc178af4631466dd82c4147c5.png)  
In this array at payload[2] , the value 300 should be changed with the value injected with one of the inject nodes (for example 1000).  
Then the array should be rewritten with this new value to the same file.  
Concretely if I send 1000, I want to get auto\_config\_delay=1000 instead of auto\_config\_delay=300.  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/9/890f743611c7866390bed052a7cc452369d59fdc.png)

I can see a function node, but my javascript skills have not evolved.  
I've seen and read some examples but I just can't do it.  
Thanks for advises.

---

<div class="post-metadata">

**Author:** ![UnborN](https://avatars.discourse-cdn.com/v4/letter/u/4491bb/32.png) [@UnborN](https://discourse.nodered.org/u/UnborN)\
**Post date:** [7 January 2022 21:47 UTC](https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268/2 "2022-01-07T21:47:31Z")

</div>

Hello Jean,

I came up with this JS

```auto
let lines = msg.payload.split("\n")
node.warn(lines)

lines.forEach((line, index) => {
    if (line.startsWith("auto_config_delay")) {
        lines[index] = "auto_config_delay=" + msg.delay;
    }
})

msg.payload = lines.join("\n")

return msg;

```

**[EDIT]** Or by using regular expressions

```auto
msg.payload = msg.payload.replace(/(auto_config_delay=)(\d+)/, `$1${msg.delay}` )
return msg;

```

**Test flow** (... change the path in the File read node)

```auto
[{"id":"cbd871c9b60dd253","type":"inject","z":"54efb553244c241f","name":"1000","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"delay","v":"1000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":210,"y":780,"wires":[["3fe504116b9214f4"]]},{"id":"3fe504116b9214f4","type":"file in","z":"54efb553244c241f","name":"","filename":"c:\\users\\user\\desktop\\raspiwifi.conf","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":480,"y":820,"wires":[["48d7e0382b2e1ae0","e4b016f58f37a2b4"]]},{"id":"10b68ecfef925297","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":870,"y":780,"wires":[]},{"id":"58171be59591a3a9","type":"inject","z":"54efb553244c241f","name":"3000","props":[{"p":"payload"},{"p":"topic","vt":"str"},{"p":"delay","v":"3000","vt":"num"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payloadType":"date","x":210,"y":880,"wires":[["3fe504116b9214f4"]]},{"id":"e4b016f58f37a2b4","type":"function","z":"54efb553244c241f","name":"forEach","func":"let lines = msg.payload.split(\"\\n\")\n//node.warn(lines)\n\nlines.forEach((line, index) => {\n if (line.startsWith(\"auto_config_delay\")) {\n lines[index] = \"auto_config_delay=\" + msg.delay;\n }\n})\n\nmsg.payload = lines.join(\"\\n\")\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":720,"y":780,"wires":[["10b68ecfef925297"]]},{"id":"a04535d87d3024f8","type":"debug","z":"54efb553244c241f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":870,"y":860,"wires":[]},{"id":"48d7e0382b2e1ae0","type":"function","z":"54efb553244c241f","name":"Regex","func":"msg.payload = msg.payload.replace(/(auto_config_delay=)(\\d+)/, `$1${msg.delay}` )\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":710,"y":860,"wires":[["a04535d87d3024f8"]]}]

```

---

<div class="post-metadata">

**Author:** ![ghayne](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/ghayne/32/39_2.png) [@ghayne](https://discourse.nodered.org/u/ghayne)\
**Post date:** [7 January 2022 21:52 UTC](https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268/3 "2022-01-07T21:52:42Z")

</div>

You could also use the "sed" command, which you could call from an exec node with relevant parameters.

> **[How to use sed to find and replace text in files in Linux / Unix shell](https://www.cyberciti.biz/faq/how-to-use-sed-to-find-and-replace-text-in-files-in-linux-unix-shell/)**
>
> sed find and replace text command: This guide explains how to use sed to find and replace text within a file on a Linux, macOS, \*BSD and Unix

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [7 January 2022 22:43 UTC](https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268/4 "2022-01-07T22:43:13Z")

</div>

2 exciting solutions. The first one is well integrated and the other one opens new horizons. Which one to choose in this case. There are many possibilities to use this kind of need again. The linux command is obviously easier to understand at first but the first one is well integrated in NR.  
I'm now on the night train to sleep. I will test tomorrow morning. A nice goal when I wake up.  
Tanks to @UnborN and @ghayne for the solutions.Top !

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [7 January 2022 23:12 UTC](https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268/5 "2022-01-07T23:12:43Z")

</div>

I would probably go for the exec node and linux tools too.  
Edit: changed this line to preformatted text so the backslash isn't removed. (or is it a forward slash?)  
`sed -i '/auto_config_delay/ s/[0-9]\+/555/' /etc/raspiwifi/raspiwifi.conf`

An example of constructing the command in a function and passing it to exec:

```auto
[
    {
        "id": "610783a98cdbdc04",
        "type": "exec",
        "z": "a5c379cc01191edc",
        "command": "sed -i ",
        "addpay": "payload",
        "append": "",
        "useSpawn": "false",
        "timer": "",
        "winHide": false,
        "oldrc": false,
        "name": "",
        "x": 430,
        "y": 140,
        "wires": [
            [],
            [],
            []
        ]
    },
    {
        "id": "eec76c67df63ade2",
        "type": "function",
        "z": "a5c379cc01191edc",
        "name": "",
        "func": "let file = \"/home/pi/foo\";\nlet key = \"auto_config_delay\"\nlet newvalue = 555\nmsg.payload = \"'/\" + key + \"/ s/[0-9]\\\\+/\" + newvalue + \"/' \"+ file;\nreturn msg;",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 280,
        "y": 140,
        "wires": [
            [
                "610783a98cdbdc04"
            ]
        ]
    },
    {
        "id": "20e3fcd9a22e8711",
        "type": "inject",
        "z": "a5c379cc01191edc",
        "name": "",
        "props": [
            {
                "p": "payload"
            },
            {
                "p": "topic",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "payload": "",
        "payloadType": "date",
        "x": 120,
        "y": 140,
        "wires": [
            [
                "eec76c67df63ade2"
            ]
        ]
    }
]

```

But since the file is in /etc you might need to use sudo.

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [8 January 2022 09:07 UTC](https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268/6 "2022-01-08T09:07:00Z")

</div>

😃  
So I tested the 2 solutions, the simplest being the Linux command.  
It is clear that if I am under Windows it is not the right plan.  
The JS proposal would be the best.  
As pointed out, the user doesn't need to enter a password, so:

```auto
sudo visudo and insert at the section #includedir /etc/sudoers.d
pi ALL=(ALL) NOPASSWD:ALL in the section 

```

![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/0/80e97c8d7496c24e6bbfa6c6d510d3b40036af2d.png)

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/6/f/6fd88506b72b04852543efc826f3a5bf2f840ea3.png)

That's great, **thank you gentlemen** for your help. I will be able to implement this this morning in my hybrid inverter management software.  
A software that I will present to you soon in the projects section.  
It will be free because it is thanks to Node Red and this forum that I made it.

_Edit: It should be possible to select several solutions_ 👏

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [8 January 2022 09:30 UTC](https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268/7 "2022-01-08T09:30:41Z")

</div>

> [@Jean-Luc](#):
>
> `pi ALL=(ALL) NOPASSWD:ALL`

I have to point out that that will let the default user running node red or any s/w you may install or gets onto your machine, run any system command, including, for a example, a command to erase the complete disc. You, might be better to use something like  
pi ALL=NOPASSWD:/bin/sed  
That only allows that command to be run without pwd.

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [8 January 2022 09:44 UTC](https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268/8 "2022-01-08T09:44:41Z")

</div>

> [@Colin](#):
>
> I have to point

Yes @Colin you are perfectly right to point out this command, I will immediately change these rights. I didn't know you could be granular with this order.

---

<div class="post-metadata">

**Author:** ![jbudd](https://avatars.discourse-cdn.com/v4/letter/j/5f8ce5/32.png) [@jbudd](https://discourse.nodered.org/u/jbudd)\
**Post date:** [8 January 2022 09:51 UTC](https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268/9 "2022-01-08T09:51:28Z")

</div>

> [@Colin](#):
>
> pi ALL=NOPASSWD:/bin/sed  
> That only allows that command to be run without pwd.

But one could do plenty of damage with sed. eg **sed 's/.\*//' /etc/passwd**

Better if possible to have the file owned by the Node-red user and in a directory below ~/.node-red

---

<div class="post-metadata">

**Author:** ![Jean-Luc](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/jean-luc/32/89996_2.png) [@Jean-Luc](https://discourse.nodered.org/u/Jean-Luc)\
**Post date:** [8 January 2022 10:37 UTC](https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268/10 "2022-01-08T10:37:20Z")

</div>

The file I have to change comes from this great software that allows to put the Pi in wifi A.P. mode.  
So I don't have the hand on its location.

> **[GitHub - jasbur/RaspiWiFi: Headless WiFi configuration for the Raspberry Pi...](https://github.com/jasbur/RaspiWiFi)**
>
> Headless WiFi configuration for the Raspberry Pi (or most other devices running Linux) by using a temporary WiFi access point and web interface - GitHub - jasbur/RaspiWiFi: Headless WiFi configurat...

---

<div class="post-metadata">

**Author:** ![dceejay](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/dceejay/32/38_2.png) [@dceejay](https://discourse.nodered.org/u/dceejay)\
**Post date:** [8 January 2022 10:46 UTC](https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268/11 "2022-01-08T10:46:37Z")

</div>

You may be able to create a symbolic link from one location to the other - but yes for your situation given that it is a private network and hopefully not exposed to the internet then sed having elevated privileges is probably low on the list of concerns if intruders get in.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 January 2022 10:46 UTC](https://discourse.nodered.org/t/change-a-value-into-a-text-file/56268/12 "2022-01-22T10:46:58Z")

</div>

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