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.
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.
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).
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:
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?
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.
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.
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.