It is currently March 28th, 2024, 5:52 pm

Replacing line in a text file?

Discuss the use of Lua in Script measures.
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Replacing line in a text file?

Post by StArL0rd84 »

So i am making a voice assistant skin based on the plugin dgrace made.
https://forum.rainmeter.net/viewtopic.php?f=18&t=21942&p=130729&hilit=voicecontrol#p130729
But with a lot more functionality.

For the best result it'll requires a grammar file (.grxml) with key words to match the voice commands specified in the plugin measure.
To make it easy on a user of my skin i want to automate this process of renaming inside the .grxml text file.
Found a light command line utility called F.A.R.T. that can Find And Replace Text.
Works great within rainmeter and it looks something like this:
[#@#exe\Fart\fart.exe "#@#VoiceControl\RmCommands.grxml" "OLD TEXT" "NEW TEXT"]

But i wanna ditch this method because if more than one line contain the words OLD TEXT, all of them will be replaced.
So i started looking around for something that could simply replace an entire line of text, just by line number.
Weeell this is where i get lost because of the lack of coding skills :/

Found some lua scripts in the Rainmeter documentation that looks promising.
https://docs.rainmeter.net/snippets/read-write-file/
But i have no idea how to use it.

Any advice or example how i could implement would be appreciated.
Thanks.
Last edited by StArL0rd84 on January 7th, 2017, 2:23 pm, edited 1 time in total.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Replacing line in a text file?

Post by jsmorley »

The Lua to simply replace an entire line of text in a file by line number is not difficult, however it would help if I knew a bit more about how you intend to implement this. The trickier bit may well be the presentation and interaction with the user.
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Replacing line in a text file?

Post by StArL0rd84 »

jsmorley wrote:The Lua to simply replace an entire line of text in a file by line number is not difficult, however it would help if I knew a bit more about how you intend to implement this. The trickier bit may well be the presentation and interaction with the user.
For example you could say "open games" where 'games' is a folder name in my folder-skin in the suite.
and the folder name is obtained using rainfile when the user picked the folder.
so im imagining that with the action of picking the folder with rainfile, the .grxml will be updated with the new line containing the new folder name.

Line looks like this:
<item> games </item>

Line number 182
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Replacing line in a text file?

Post by jsmorley »

StArL0rd84 wrote:For example you could say "open games" where 'games' is a folder name in my folder-skin in the suite.
and the folder name is obtained using rainfile when the user picked the folder.
so im imagining that with the action of picking the folder with rainfile, the .grxml will be updated with the new line containing the new folder name.
Not sure I entirely follow. Don't see how this is related to any particular "line number" in the text file. At first blush, still sounds like a global search-and-replace, which is what you don't want.

You have some example?
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Replacing line in a text file?

Post by jsmorley »

Seems to me that if the lines are all like:

<item> games </item>

then a global search and replace will still be fine, you just have to search and replace on

<item> SomeText </item>

and replace it with

<item> SomeNewText </item>
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Replacing line in a text file?

Post by jsmorley »

Of course, I would still use Lua over an external executable in this case....
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Replacing line in a text file?

Post by StArL0rd84 »

jsmorley wrote:Not sure I entirely follow. Don't see how this is related to any particular "line number" in the text file. At first blush, still sounds like a global search-and-replace, which is what you don't want.

You have some example?
ill try to explain the steps of events.

1. Folder is changed with rainfile.
["#RainFile#" "Folder" "Variables" "Fpath7" "#Var#" "C\"]

2. A path parser script obtains the name from the path.

Code: Select all

[mFolderPathParser7]
Measure=Script
ScriptFile=#@#lua\PathParser.lua
Input=#Fpath7#.exe
Output=Name
So im thinking whenever folder 7's name changes, the .grxml should be updated.
I know which line of the .grxml should be changed which is line number 182.
Cant the lua somehow index or count the lines and then replace?
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Replacing line in a text file?

Post by jsmorley »

StArL0rd84 wrote:ill try to explain the steps of events.

1. Folder is changed with rainfile.
["#RainFile#" "Folder" "Variables" "Fpath7" "#Var#" "C\"]

2. A path parser script obtains the name from the path.

Code: Select all

[mFolderPathParser7]
Measure=Script
ScriptFile=#@#lua\PathParser.lua
Input=#Fpath7#.exe
Output=Name
So im thinking whenever folder 7's name changes, the .grxml should be updated.
I know which line of the .grxml should be changed which is line number 182.
Cant the lua somehow index or count the lines and then replace?
Sure, that's not hard at all in Lua, but I don't see how it knows that anything is line number 182... I'm missing the logic here. I don't see what line 182 has to do with it.
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Replacing line in a text file?

Post by jsmorley »

I'm not just being a pain in the neck here, it's important that we walk through the logic so we have a full trail from point A to point Z. Is my Lua going to look like:

function textReplace(lineNumber, oldText, newText)

or

function textReplace(oldText, newText)

It matters....
User avatar
StArL0rd84
Posts: 424
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: Replacing line in a text file?

Post by StArL0rd84 »

The script should not care what is at line 182. just delete it and write new text.
And you're not being a pain, i just dont know what you need from me :)
182.png
Post Reply