Hi, trying to make a list, started doing some grep and regex and thought it'd be easier with KM
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:
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.
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
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.
Sounds like a lot of work to me, but if you're happy with it, who am I to complain?
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.
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
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>:
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) 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.