How Do I Replace a Line in a Text File, using KM or Perl?

I have this action in a KM macro:

perl -p -i -e 's/CreateDate=.*/CreateDate=20200101T120001Z/' "$KMVAR_myFile"

I'd like to use today's date and time in the replacement string (yyyymmddThhmmssZ).

I'd be grateful for a way to do this either via Perl or by passing KM date and time variables to the Perl script. Would this be something like:

perl -p -i -e 's/CreateDate=.*/CreateDate='&"$KMVAR_myDateTime"&'/' "$KMVAR_myFile"

Is there some reason you can't do this simple change using KM non-script Action?
What is the objective of your perl script?

Having said that, for how to use KM Variables in shell scripts, see:
Execute a Shell Script action.

The reason why I'm using a Perl script to use some settings in a settings file, is that some kind person has given me this script (without the regular expression part) and that I'm not aware of a simple way change these settings via KM directly.

I've read the Wiki article on Shell script actions. Since I suspect that the quotes are causing troubles here, I decided to put as much as possible in a new KM variable myCommand.

Alas, this doesn't work either.

The macro is executed on a folder on my Desktop, that only contains a PRJ file with some settings.

Test PRJ file changing.kmmacros (5.0 KB)

Test_folder.zip (1.2 KB)

We have several shell scripting gurus as members here, so hopefully one will come along that can answer your question.

If you'd like to share exactly what you are trying to get from the Perl Script, perhaps we could offer an alternative using native KM Actions and/or ASObjC.

I'm not entirely clear on what you're up to, but see if this doesn't get you there:

Add Date & Time to Filename Macro (v9.0.4)

Add Date & Time to Filename.kmmacros (2.8 KB)

In this case, it returns (or prints) the path and a new filename based on the date Test.20191228T094840.PRJ.

I used a Prompt for File action instead of your logic for my own convenience. The variable test_kmVar contains the date string I believe you want to add to the current file.

The Perl reads the Keyboard Maestro variables as environment variables and constructs a variable with the new name. Presumably, your existing Perl script does something with that variable so you can just pop the existing Perl code into the Execute Shell Script action (and get rid of the print statement).

Hope that helps.

1 Like

Sorry for being unclear.

What I'm trying to do is: have the line LastChangedDate=20200101T120001Z in the file Test.prj (file name is stored in KMvar_myFile) changed to LastChangedDate=20191230T110011Z

From your example I learn that I can reference to the set date variable via ${date}. But obviously I'm doing something wrong here:

This doesn't work either:

s/LastChangedDate=.*/LastChangedDate=${date}/ ${fn}

So, to make sure we understand your objective, is it correct that you’re trying to change the contents of the file, and not the file name?
If so, that means your file is a text file, right?

Indeed. It is a plain text file (though with the extension PRJ). I had it attached to my first posting:

To change a text file, I would use the KM action search and replace, using a file as the source.

https://wiki.keyboardmaestro.com/action/Search_and_Replace

Am I right that there is no ready made KM feature to read all lines of a text file one after another?

OK, got it. You want to edit one line in the file itself. Here you go:

Change Line in File Macro (v9.0.4)

Change Line in File.kmmacros (3.0 KB)

If you want feedback from this macro, just add print $file_contents; to the end of the script. I stripped all that out after testing.

BTW, the code you were working with was a one-liner (that really doesn't do anything but echo a variable it can't find) you would use from the command line in, say, Terminal. What I've given you is a commented Perl script that reads a file, makes one change to the contents and writes it back out again.

1 Like

Yes there is, but you don't need that to make the changes you want.

Very easy.

Example Output AFTER Change

image

MACRO:   Change Text in File [Example]

--- VER: 1.0    2019-12-30 ---
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Change Text in File [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Step Through All Lines in File

image

See For Each action

2 Likes

Thank you! Very useful and compact.

Thank you. I'll use this to create a KM-only approach to create a generic macro to change lines in plain text files, e.g. to set parameters in INI files.

1 Like

I hope you don't mind that I have revised your topic title to better reflect the question you have asked.

FROM:
Get today's date and time in Perl replacement string

TO:
How Do I Replace a Line in a Text File, using KM or Perl?

This will greatly help you attract more experienced users to help solve your problem, and will help future readers find your question, and the solution.

1 Like