I'm trying to create an Automator Folder Action that does the following when multiple files are moved into it. Each file has the same format (TextA is always the same; StringA and B are different for each file). But the data I want is also duplicated in each file, and I only want one instance of it:
File1.txt
TextAfile1 StringAfile1
TextBfile1 StringBfile1
TextAfile1 StringAfile1
TextBfile1 StringBfile1
An actual example of the text would be:
File1.txt
The delivery of TitleA
The barcode for this is 1234
The delivery of TitleA
The barcode for this is 1234
File2.txt
The delivery of TitleB
The barcode for this is 5678
The delivery of TitleB
The barcode for this is 5678
My automator flow is this so far (Folder action receives files added to Folder):
- Run Shell Script (display 2 single lines of text starting with "TextA" and "TextB")
grep -i 'The delivery of' "$@"
grep -i 'The barcode' "$@"
New Text File (plain txt)
Loop through all files (automator seems to do this automatically)
Run Shell Script (Delete "TextA" and TextB from the beginning of all lines so they all start with the text StringA that immediately follows)
sed -e "s/The delivery of //g" "$@" sed -e "s/The barcode for this is //g" "$@"
Run Shell Script (alphabetize sort)
cat "$@" | sort
The resulting Text file should look like this:
TitleA 1234
TitleB 5678
...
TitleZ ####
Right now I'm getting this (without quotes):
"/users/path/to/file1.txt:The delivery of TitleA"
"/users/path/to/file1.txt:The delivery of TitleA"
"/users/path/to/file2.txt:The delivery of TitleB"
"/users/path/to/file2.txt:The delivery of TitleB"
"/users/path/to/file1.txt:The barcode for this is 1234"
"/users/path/to/file1.txt:The barcode for this is 1234"
"/users/path/to/file2.txt:The barcode for this is 5678"
"/users/path/to/file2.txt:The barcode for this is 5678"
I'm trying to delete the path, delete the preceding text, and remove the duplicates. So ideally I'll end up with:
TitleA 1234
TitleB 5678
Bonus would be to also sort the final txt file alphabetically. But I could settle for pasting it into Excel and sorting there etc. although sorting in script would be better if not too dificult.
Am I on the right track at all? I've tried all different combinations of this workflow and it feels like there's a fundamental flaw with it.