[Solved] Find contents in files, create file

Hi, trying to make a list, started doing some grep and regex and thought it'd be easier with KM :slight_smile:

So, a folder has some XML files:

file1
file2
file3
etc

I'd like to process each file, and get the following for each one:

1. Extract a string inside each file
This grep works in Terminal:
grep -E "name-en([^>]+)"\surl="

2. The number of occurrences of a string in each file
This command works in Terminal:
grep -RIci "</Preset>" . | awk -v FS=":" -v OFS="\t" '{ print $2, $1 }'

So, my goal is to create a file result.txt with the following content:

name-en-string-result 10 file1
name-en-string-result 8 file2
name-en-string-result 0 file3
name-en-string-result 1 file4
etc

forgot the KM screenshot :sweat:

You might take a look at Textcavator for inspiration if nothing else. It does use Keyboard Maestro to find text in a directory of documents and report counts and sources with snippets in context (not quite the output you're looking for admittedly).

As shipped, it does not look at .xml documents but that's easy to add. The documentation explains just how.

This is easy in KM using the Search using Regular Expression action

image

But counting RegEx matches can't be done directly in KM. So I'd use another tool for counting. My pref would be JXA, but since you know grep, then you can just use that in a Execute a Shell Script action

thanks both!
don't know Regex, just a little bit :slight_smile:

If you will provide some detailed, real-world, examples of your source data (attach your file as a zip if you like) and exactly what you want the results to be, both in Code Blocks (or the actual file) along with the details of what text you want to extract, and what text you want to count, I'll take a look at it.

I don't know anything about grep, so I can't even read your code.

1 Like

Solved!
Ended up doing it in Excel importing files as Data, splitting columns in there, and using the second grep to complete the information.

Example file:
https://raw.githubusercontent.com/earthbound19/filterForgeBatchWrapper/master/Batik%20Art%20Mod.ffxml

just will need to re-run the second grep from time to time to generate an updated output.txt file that I use for input in Excel

Sounds like a lot of work to me, but if you're happy with it, who am I to complain? :wink:

If you don't need the data in Excel for other reasons, seems like to me we were close to a KM solution that could be completely automated. As they say at Motel 6, we'll leave the lights on for you if you decide to come back. :smile:

thanks, I'll reopen this tomorrow and try to get it done in KM, some things are getting complicated in Excel...

question, why the extra F in %FfilePath% ? both seem to return the same.
Also, how to also get other filetypes in the loop? seems the For Each command only goes through TXT files

Actually that is a typo. You should change it to
FilePath
in all Actions.

Nope, it should loop through all files in the folder, regardless of file type.
Note that in my example I left the folder/directory path blank.

great! with your feedback got the first grep working!,

Now trying to get the second part to work.

(tested the Execute shell command, but the script didn't seem to work. Plus, I'm not well versed in grep/awk, so tried to look for alternate method).

Found this thread and the counter is working!, opened a file and pasted its content inside, and displays the number of occurrences of the word </Preset>:

Summary

I also found a thread about splitting path and filename... which I seem to have lost.

EDIT2: Nevermind, found a thread about Get File Attribute :smiley:

EDIT: Almost there, needed to change block to this:

@hello (did you really intend for that to be your screen name?),

It's not clear if you still have questions. If so, feel free to post.

The script worked great, it created a neat file. example1.txt.zip (6.7 KB)

After that, I made another script: it takes elements of the TXT to create a XML file (for the command line) :smiley: it's a very simple script for now. Ideally it'd use dropdowns to make it friendlier and speed up the process, but I think that's a topic for new thread.

saw the username was available :slight_smile: