[Solved] How to search and replace a variable value from a list in text file?

I have two text files with a list of URLs.
I need to pick first URL from file A, use it in my macro and then append the file B with that URL and delete that URL from file A.
(Basically file B would act kind of like a log of which URL I have used.)

So what I initially planned was,
Using 'for each' action to get URLs from file A. Use the URL in my action steps. Where I'd perform the action steps for each %Variable%ShortVideoURL% in file A. And then search for it (%Variable%ShortVideoURL%%LineFeed%) in the text file and replace it with blank.

But Search and Replace action doesn't let us search for a token.

How to approach this?
Just in case, here's how both files would like like with URLs.
Screenshot 2023-08-26 at 11.33.42

TLDR; version


Trying to delete URL from the list in the text file. But this action from the screenshot is not giving results.

To read lines from a file one at a time and then remove that line and repeat the process until there are no more lines left you should take a look at my subroutine that I posted here;

To create the “log” you can simply append the line you got from the subroutine to a file of your choosing using the Append to File action action:Write to a File [Keyboard Maestro Wiki]

I’ll also point you to a very similar question you asked before:

To demonstrate the use of the subroutine, here's a macro I've put together that does what you're asking:

Download Macro(s): Test Read File Line by Line and Log.kmmacros (4.6 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 10.15.7
  • Keyboard Maestro v10.2

Please note the following:

  1. You need to download and install the subroutine I linked to above.
  2. The two actions coloured red need to be filled in with your two filename paths: the first the input file and the second the log file.
  3. The group action coloured orange - you will need to put your desired processing of each line of text obtained from the file here. The variable containing the text is called Local__LineToProcess

I hope you find this useful.

Damn

I read your edited reply later. Might have saved me hours.

I already am using your steps in some of my macro. (You later mentioned my own question from the past). It didn't come to my mind that I can use steps from there.

Later when you shared your macro (your initial response), I spend hours figuring out how to use it. Now I finally could do it and came here to tell you. Only to find you have uploaded a ready macro for me :sweat_smile:

Thank you so much, as always!

1 Like

I have a question. So as I mentioned earlier, I had figured something out and somehow the macro started working as I wanted it to, before I saw the macro you shared.

In your macro, you used the subroutine twice. I used once and it still works. How is that possible? Am I missing something? Is it going to create any problems later if I continue using my macro? Can you please enlighten me?

Let me share subroutine and main macro.
FS old upload.kmmacros (4.2 KB)
Screenshot:

FS old upload subroutine.kmmacros (11.1 KB)
Screenshot(1/2):


Screenshot(2/2):

What I can see just by glancing at your screenshots is enough to raise the following point: since you don’t seem to have any kind of loop in your macro it will process just one line from your list file each time you trigger it. Is that correct or am I missing something?

My macro does have a loop and processes all the lines in your list file with just a single triggering which is what I thought you wanted from your original post.

One last point: I’m surprised to see you’ve changed my original subroutine so it uses global rather than local variables. I wouldn’t be prepared to guarantee its correct operation in those circumstances so I hope you’re aware of the possible side-effects of doing that!

Since I’m out of the office right now I won’t be trying to examine your macros in any further detail and anyway you say they work so what is there to debug?

Okay gotcha
I didn't realise the difference is your macro has a loop.
One reason my macro doesn't have a loop is because I couldn't figure out how to create a loop (like you did). And also I wanted to have some kind of control over how many times it repeats.
I am planning to put all the action steps in 'loop' action with variable determining how many times it should loop.

The reason why I did that is because as far as I remember, when I had your your macro few weeks back, it didn't work because it had local variables and the value wasn't being used in the main macro. I am sure you'd not create a kind of macro that won't work but looks like I was missing something or I had some another macro in which I needed these variables to work as well.

But anyways for this use case I tried your macro with local variables and it is working fine.

And I am not aware of the side effects. Could you possibly shade some light on it?

Because I have a huge scope to learn and I might be making some mistakes although the macro could be working okay. Take for example of the side effects. I wasn't aware they exist. And they would have still be unknown to me if I hadn't ask.

In your OP it says you want to do actions for each line in your input file. Isn’t the number of lines then also the number of times it repeats? My use of the While action (which is the loop) ensures that while the line read from the file contains something then the actions in the orange-coloured group will be performed. There’s no need for a separate mechanism to set the number of repetitions. Maybe I’m missing something here…?

I apologise for using terms you’re not familiar with - sometimes I forget that everyone is on a different part of the learning curve :slightly_smiling_face:

l’m talking specifically about global variables: suppose you’ve written macroA and it uses within it a global variable called X. So far so good. But then at some later date you create macroB and that also uses a variable called X. Now, each of the two macros can read and change the value of that variable and the side effect is this: when macroA runs it may be that the value of X it is using might have been set by macroB and similarly macroB might be using a value of X that has been set by macroA. In other words - the two macros can unintentionally interfere with each other.

Sometimes this is a desirable behaviour such as when you wish to pass a value from one macro to another. But more often than not (and it does happen a lot) people use same-named variables and are puzzled when a macro misbehaves because a particular variable doesn’t have the value that they’re expecting it to only to discover that another macro has changed that value unexpectedly.

You might think “this will never happen to me” but the more macros you create and the more global variables you use will almost certainly result in this happening!

I personally always use local variables since they prevent this kind of side effect which is why I wouldn’t be able to guarantee the correct operation of that subroutine since I designed it to work with local variables and tested it with same. So how it behaves with global variables is something I’ve never considered nor tested.

This is a bit long-winded and reading it back I’m not sure how clearly I’ve explained it but it’s all part of something called “scope of variables” which you can read about in the KM wiki page about variables.

Yes, my bad. I changed my mind later.

I simply want to control how many times it repeats. My main purpose may not be to finish processing on ALL the URLs from file A.
Because the process may take time and I might resume processing the remaining URLs next time.

Gotcha.
Yes I am aware of this and have faced problems due to this in the past.
I was not very serious about it until you expressed that I might think "this will never happen to me". Thanks for the heads up.
And thank you for having the detailed approach to the explaining. It helps.

That's very straightforward. If you haven't already worked it out I'll post my example.

I think i'll be able to figure this out. In case I can't, I'll ping you here again.

Thanks for your readiness to help :blush:

1 Like