Extract text from Illustrator file using shell script

Since Illustrator CC 2018, Adobe no longer writes the file version into the resource fork. Unfortunately, my scripting environment relies on reading this to record the information in our artwork slug.

I can’t find a solution to this, but I have an idea… When I open the .ai file in TextEdit, I can see a line beginning with "%%Creator: Adobe Illustrator(R) " and then a number. That is 15.0 for CS 5, and 17.0 for CC.

I would like to extract the text from this line using a shell script, but my knowledge of this is more limited than Applescript! What I have here doesn’t work:


set theFileVersion to do shell script "grep '%%Creator:' /Users/username/Desktop/test.ai | pbcopy "
log theFileVersion

Thanks in advance for any help

Right!
I have found something that works, if anyone is interested


set search_command to "awk '/%%Creator/ { print $5 }' /Users/username/Desktop/test.ai "
set theFileVersion to do shell script search_command

This returns:
“17.0
%%AI8_CreatorVersion:”

The number is what I want, and I can use applescript to get rid of the trailing text.
…unless anyone knows how to do this with awk or another command line thingy.

For anyone wracking their brains to learn this stuff, I found the most useful info here:
https://www.lifewire.com/write-awk-commands-and-scripts-2200573

My awk knowledge is also not particularly extensive; that said, I saw that your result anomalously occupies more than one line after accessing the fifth found item, so I played around with it. Your implementation appears to actually be ignoring the pattern after the first “%,” and it just happens to be returning something approximating the desired result. The problem stems from unexpected line endings, and it can be corrected as below. Full disclosure… I also got some help from reading this post: https://stackoverflow.com/questions/44510813/changing-the-field-separator-of-awk-to-newline

#this respects the pattern

do shell script "awk 'BEGIN{ RS = \"\\r\"  ; FS = \"\\n\" }  /%%Creator: Adobe Illustrator[(]R[)] / {print $0}' " & (choose file)'s POSIX path's quoted form   --->"%%Creator: Adobe Illustrator(R) 13.0"

–or:

#this alternate will isolate the number portion

do shell script "awk 'BEGIN{ RS = \"\\r\"  ; FS = \"\\n\" }  /%%Creator: Adobe Illustrator[(]R[) ]/ {print substr($0,33,4) }' " & (choose file)'s POSIX path's quoted form  --->13.0

–Edit: $1 changed to $0; the former worked, but there is only one found item

Thanks Marc

Your awk code worked a treat.