Script Help!? Delete if line starts with 'xx'

I have a bunch of Text Edit files that are filled with tech-speak, and I would like to filter and delete items that do not begin with one of two specific phrases:
^fi( or ^bl(

Is there an AppleScript person who can point me in the right direction?
(This is part of a larger KM macro)

Please post some specific examples....two to keep and two to delete
Also how do you know the end of the phrase??

I just started to learn programming languages weeks ago. Still a newbie.

I tried the following, but found only the first line (shown below). I would like to learn why if someone can offer an explanation.
image

So I tried Python instead.
It seems to work.
Here is my code:

#!/usr/local/bin/python3
import os
import re

# get variable from KM. The variable Var is shown above.
text = os.environ['KMVAR_Var']

# find all lines we want
linesFound = re.findall('(?m)^(fi.*?)$|^(bl.*?)$', text)

"""
The above command will return linesFound = [('fi--this is what I want', ''), ('', 'bl--this is also what I want'), ('fi--this is what I want 2', ''), ('', 'bl--this is also what I want 2')]
So, we have extra work to do. Each element is a list, but one of them has nothing. Below, We just replace the list with the other non-blank string.
"""

for i in range(0, len(linesFound)):
	line = "".join(linesFound[i])
	linesFound[i] = line
"""
The above loop will return: linesFound = ['fi--this is what I want', 'bl--this is also what I want', 'fi--this is what I want 2', 'bl--this is also what I want 2']
"""

# finally, covert 'linesFound' to a string, with line break added between each line
linesWanted = '\n'.join(linesFound)

print(linesWanted)

output:

fi--this is what I want
bl--this is also what I want
fi--this is what I want 2
bl--this is also what I want 2

I'm also a newbie in AppleScript. I will just give up trying. If you have to use AppleScript, someone else may be able to help.

As a newbie. your are being fairly adventuresome...
I am not a regex expert but please see part 2 of

as it gives some useful sources that may help you.

One of my colleagues may help you further

I've discovered in the meantime that the text files are organized in a way that I don't need to filter them. The spots to delete are all lumped together, and can be found then โ‡งโŒ˜-up arrow and delete etc. with just KM actions.

But, a script lesson is still useful!
so here are the answers to your questions:

There is no end of phrase - I would need to filter the entire Text Edit file.

nice - I'm more newbie than you, but I'll give this a go.

welcome to the forum, let us know if you need any further help

1 Like

Newbie to scripting...not to this forum!

After you paste your source text, I saw the ^ sign is part of the text, not the beginning of line sign in RegEx. So, I need to adjust the RegEx search line in Python to:

# find all lines we want
linesFound = re.findall('(?m)^(\^fi.*?)$|^(\^bl.*?)$', text)

adding \^ to the expression.
This will finally return:

^fi--this is what I want
^bl--this is also what I want
^fi--this is what I want 2
^bl--this is also what I want 2

You probably don't need an actual script (or do you mean "Macro") for this.
From the looks of your image, it should be an easy RegEx.
But, as always, please post as TEXT the following, using the forum Code Block with "text" as the language:

  1. Actual source text
  2. Final Resultant text that you want