0

I have a file that an application has set extended attributes for.

$ xattr -l /Volumes/Educating\ Rita\ \(1983\)/Educating\ Rita\ \(1983\).1.eng.srt
net.filebot.filename: 3_English.srt
net.filebot.metadata: {"@type":"Movie","year":1983,"imdbId":85478,"tmdbId":38291,"language":"en","id":38291,"name":"Educating Rita","aliasNames":[]}

I have tried a few different ways to get the value of @type inside the attribute but haven't found a way to search multiple files and grep for and return those values.

xattr -l /Volumes/Educating\ Rita\ \(1983\)/Educating\ Rita\ \(1983\).1.eng.srt | grep @type
net.filebot.metadata: {"@type":"Movie","year":1983,"imdbId":85478,"tmdbId":38291,"language":"en","id":38291,"name":"Educating Rita","aliasNames":[]}

I want to find every .srt file and grep for the value of @type. In the above results the command would return "Movie".

$ find . -iname "*.srt" | while read -r file; do `xattr -l "$file" | grep  "Type" >> /Users/john/Desktop/filebotmeta1.txt` ; done

That gives me a text document with this content:

net.filebot.metadata: {"@type":"Movie","year":1983,"imdbId":85478,"tmdbId":38291,"language":"en","id":38291,"name":"Educating Rita","aliasNames":[]}

But I want to return @type and it's value not the entire contents of the attribute.


I am not sure getxattr or listxattr apply to the command line. They seem to be for internal use only.

I imagine this involves outputting to a .tmp file and then grepping the contents of the file? Or, is there a one line argument I can use to print results in Terminal?

The contents look like json format.

2

If you could be sure the file were JSON format, there might be a better way to do this, but you'd probably need to know something of the structure of the JSON records.

However, if I understand your question correctly, I think you can get what you want with grep if you use the -o or --only-matching option (see man grep for details)

# create a string 'STRINGX' to test a regex
% STRINGX='net.filebot.metadata: {"@type":"Movie","year":1983,"imdbId":85478,"tmdbId":38291,"language":"en","id":38291,"name":"Educating Rita","aliasNames":[]}'

# try a regex that includes '"@type": plus an alphanumeric string that follows
% echo $STRINGX | grep -o '\"@type\":\"[a-zA-Z0-9]*\"'

# which yields this as output:
"@type":"Movie"

# to print only the value of @type, one option is to use sed:
% echo $STRINGX | grep -o '\"@type\":\"[a-zA-Z0-9]*\"' | sed 's/^.\{8\}//' 
"Movie" 

So - pipe your xattr -l output to the grep command as shown above.

You may be able to do a better job on the regex as I don't know if there are any rules that define characteristics such as maximum length, or presence of special characters, etc.

Let us know how this works, or if you have questions & we'll try to answer.

  • Thanks. I will try this. I was wondering if there was a specific Apple binary that allows us to query the contents of a files' extended attributes directly. Similar to the way PlistBuddy allows us to interact with .plist files. – John Sep 30 '20 at 19:14
  • @John: specific Apple binary... Ummm - don't get me started, please :) – Seamus Sep 30 '20 at 20:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .