terminal – Automator Shell Script – grep and variables – extracting and mixing textual content

on

|

views

and

comments


I am making an attempt to create an Automator Folder Motion that does the next when a number of information are moved into it. Every file has the identical format (TextA is at all times the identical; StringA and B are completely different for every file). However the knowledge I would like can be duplicated in every file, and I solely need one occasion of it:

File1.txt

TextAfile1 StringAfile1
TextBfile1 StringBfile1
TextAfile1 StringAfile1
TextBfile1 StringBfile1

An precise instance of the textual content could be:

File1.txt

The supply of TitleA
The barcode for that is 1234
The supply of TitleA
The barcode for that is 1234

File2.txt

The supply of TitleB
The barcode for that is 5678
The supply of TitleB
The barcode for that is 5678

My automator move is that this thus far (Folder motion receives information added to Folder):

  1. Run Shell Script (show 2 single traces of textual content beginning with “TextA” and “TextB”)
grep -i 'The supply of' "$@"
grep -i 'The barcode' "$@"
  1. New Textual content File (plain txt)

  2. Loop by means of all information (automator appears to do that mechanically)

  3. Run Shell Script (Delete “TextA” and TextB from the start of all traces so all of them begin with the textual content StringA that instantly follows)

    sed -e "s/The supply of //g" "$@"
    sed -e "s/The barcode for that is //g" "$@"
    
  4. Run Shell Script (alphabetize type)

    cat "$@" | type
    

The ensuing Textual content file ought to appear like this:

TitleA 1234
TitleB 5678
...
TitleZ ####

Proper now I am getting this (with out quotes):

"/customers/path/to/file1.txt:The supply of TitleA"
"/customers/path/to/file1.txt:The supply of TitleA"
"/customers/path/to/file2.txt:The supply of TitleB"
"/customers/path/to/file2.txt:The supply of TitleB"

"/customers/path/to/file1.txt:The barcode for that is 1234"
"/customers/path/to/file1.txt:The barcode for that is 1234"

"/customers/path/to/file2.txt:The barcode for that is 5678"
"/customers/path/to/file2.txt:The barcode for that is 5678"

I am making an attempt to delete the trail, delete the previous textual content, and take away the duplicates. So ideally I will find yourself with:

TitleA 1234
TitleB 5678

Bonus could be to additionally type the ultimate txt file alphabetically. However I may accept pasting it into Excel and sorting there and so on. though sorting in script could be higher if not too dificult.

Am I heading in the right direction in any respect? I’ve tried all completely different combos of this workflow and it appears like there is a basic flaw with it.

Share this
Tags

Must-read

Nvidia CEO reveals new ‘reasoning’ AI tech for self-driving vehicles | Nvidia

The billionaire boss of the chipmaker Nvidia, Jensen Huang, has unveiled new AI know-how that he says will assist self-driving vehicles assume like...

Tesla publishes analyst forecasts suggesting gross sales set to fall | Tesla

Tesla has taken the weird step of publishing gross sales forecasts that recommend 2025 deliveries might be decrease than anticipated and future years’...

5 tech tendencies we’ll be watching in 2026 | Expertise

Hi there, and welcome to TechScape. I’m your host, Blake Montgomery, wishing you a cheerful New Yr’s Eve full of cheer, champagne and...

Recent articles

More like this

LEAVE A REPLY

Please enter your comment!
Please enter your name here