The "grep -v" returns only the lines that are not in the specified string.
I haven't tested if the string can handle blanks, but it looks like you don't require blank handling.
It's too bad that the KM action above doesn't allow you to save to the same file, because it lets you save to the same variable. Seems inconsistent.
P.S. I didn't test this, because I do this sort of thing very often. I have the philosophy that answers don't have to be tested if they are probably right, or at least a very good lead to a solution.
Use Keyboard Maestro's search/replace to change the given file directly.
Use Keyboard Maestro to read the file, process the contents, and write it back to the file – by whatever means works for you.
Script it with the Shell, AppleScript, JXA, etcetera.
@Sleepy's idea is fine, although the >> operator appends to the given file.
Personally I'd use sed, because it's made precisely for such things.
srcFilePath=~/'test_directory/Source_Directory/Hosts.txt'
# cat "$srcFilePath"
sed -ni '' '/googleanalytics/d;p;' "$srcFilePath"
This script operates directly on the given file and deletes any line it finds containing the string googleanalytics – so make sure to have a backup of your data file.
Sed takes some getting used to, but it's a very mature and powerful Unix tool.
If you go with sedand want additional exclusions, you can use the -E option and add other terms to search for, with the terms separated by vertical bars. For example:
sed -Ei '' '/googleanalytics|adservice|another/d' hosts
This becomes unwieldy after more than a few terms, and then you'll probably prefer @ComplexPoint's solution.
KM also has a cool way to read commands (for sed or for most other shell commands) from an additional text box in the KM macro, similar to this, which I think is very "readable":
Note that I did not test this macro. The correct syntax depends upon the application (in this case, sed) and so this is just an example in principle of how to add multi-line parameter data to a Shell app in an easy to read manner.
The simplest working example to illustrate the idea is this:
This illustrates how the STDIN for a shell command is fed into the script.
In the case of cat, the "-" that I used is optional.
In the case of sed, the man page says that to feed the subcommands you have to use the "-f" parameter, and you probably specify the STDIN using "-" as I did.
The reason I didn't test is that I didn't want to create the files and directories that your example required.
I would like to test it, but I have a visitor coming to try to get my furnace working. It's pretty cold right now in Canada and I need to get my furnace working before I can spend much time here today.
Sed expects a file reference and chokes if it doesn't find one – nor does it recognize “-” as an operator meaning stdin.
I was able to get stdin to work but had to use process substitution to do it.
The stock shell on Mojave doesn't support process substitution. If I remember correctly Bash 4+ is required. (I have installed GNU Bash 5.1.8 on my system.)
I'm using Monterey. But this isn't a new feature of Monterey. I've used it before. Here's a screenshot showing the man page in Monterey for sed. At the bottom of the image is the description of the -f parameter which says what I quoted earlier.
That will be post Mojave. As you can see the stock Mojave version of sed is ancient, and Apple didn't even bother to give it a version number – just a date.
-f command_file
Append the editing commands found in the file command_file to the list of commands. The editing commands should each be listed on a separate
line.
BSD May 10, 2005 BSD
I'm pleased to hear that Apple has employed a more modern version of sed in later versions of macOS.
Look at the bottom of the man page and see if there's a version number. I'm guessing they're using a later version of BSD sed, but I'd like to know.
The only version number I see is the macOS version number, 12.0.
Although I see these lines, which might be different from your man pages...
STANDARDS
The sed utility is expected to be a superset of the IEEE Std 1003.2
(“POSIX.2”) specification.
AUTHORS
Diomidis D. Spinellis <dds@FreeBSD.org>
Beside the macOS version number is the date "June 10, 2020 " which I presume is when the man page was formatted. I don't imagine that's related to when the command was produced, but I guess it could be.