Egrep and $KMVAR

I'm using the Execute Shell Script action to search a directory for text files containing the values of the KM variables TextA and TextB in any order. The list of file names is then used in subsequent actions. Could any of you help me figure out why the following isn't working or provide a different method?

cd /Users/spencer/Documents/notes/
egrep -l '$KMVAR_TextA.*$KMVAR_TextB|$KMVAR_TextB.*$KMVAR_TextA' * | sed -e 's/.[^.]*$//'

I don't have any experience with this sort of thing so any help would be appreciated. I can successfully get it to work if I'm only searching for one variable, just not the combination. I suspect it has something to do with the apostrophes framing the search terms. Thanks-

In a shell script, when you use single quotes, variables will not expand, so you need to use double-quotes for the egrep command, like so try this instead:

cd "$HOME/Documents/notes/"

egrep -l "$KMVAR_TextA.*$KMVAR_TextB|$KMVAR_TextB.*$KMVAR_TextA" * \
| sed -e 's/.[^.]*$//'

Note that for the sed command, you want to make sure that you do not use double-quotes, because you want that $ to be interpreted literally, and not as part of a variable.

I also recommend using $HOME instead of /Users/spencer too. It's a good habit to get into.

1 Like

@tjluoma has given you a shell script solution, but I like to offer a KM non-scripting solution where it makes sense. As you have found, it can be complicated using scripts. Note that for a large number of files (>> 500) a script might be faster.

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.


MACRO:   Get List of FileNames That Match Criteria [Example]

-~~~ VER: 1.0    2020-03-02 ~~~

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:

Get List of FileNames That Match Criteria [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.

Example Output






  • Get List of FileNames That Match Criteria

NOTICE: This macro/script is just an Example

  • It is provided only for educational purposes, and may not be suitable for any specific purpose.
  • It has had very limited testing.
  • You need to test further before using in a production environment.
  • It does not have extensive error checking/handling.
  • It may not be complete. It is provided as an example to show you one approach to solving a problem.
  • You will need to change the first two Actions that set:
    • The Source Folder
    • The Keywords that must be in the File Name


  1. KM 8.0.2+
  • But it can be written in KM 7.3.1+
  • It is KM8 specific just because some of the Actions have changed to make things simpler, but equivalent Actions are available in KM 7.3.1.
  1. macOS 10.11.6 (El Capitan)
  • KM 8 Requires Yosemite or later, so this macro will probably run on Yosemite, but I make no guarantees. :wink:


  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. :wink:
  • Assign a Trigger to this macro.
  • Move this macro to a Macro Group that is only Active when you need this Macro.
  • ENABLE this Macro.
    • ALL Actions that are shown in the magenta color


  • While I have given this limited testing, and to the best of my knowledge it will do no harm, I cannot guarantee it.
  • If you have any doubts or questions:
    • Ask first
    • Turn on the KM Debugger from the KM Status Menu, and step through the macro, making sure you understand what it is doing with each Action.

1 Like

Wow, thank you. This helps explain quite a few actions that I didn't quite understand.

Thanks, works! I knew it was almost there!

You're welcome. As long as you remember to use double-quotes with variables, you should be OK.

If you run into a situation where you need to have both:

  1. a $VARIABLE which needs to be expanded
  2. a literal $ which should not be

then use double quotes around the whole thing and put a \ in front of the $ which should not be expanded.