I have a bunch of files that contain yaml. Some files are yaml-only, some have yaml front-matter. I would like to be able to query the list of files to return a list that match certain criteria. In other words I would like a solution that works along the lines of the grep command when using the -l, --files-with-matches
option to return the matching filenames only.
Typically the queries I would want to run against the files are:
- Does key k exist in file?
- Does key k exist in file with value v?
I am aware of the yq utility which is able to return the results of a query made against a yaml file but I don't want to do this. I simply want to be able to pass a query and get a list of matching files returned. It seems to me that yq eval should be able to do this but I can't figure out exactly what to do.
This is the sort of thing I have in my mind but I can't quite put all the pieces together:
find -name '*.txt' | xargs yq '.foo.bar' # output filename if key bar exists under key foo
find -name '*.txt' | xargs yq 'has(.foo.bar) == 12' # output filename if key bar exists under key foo and has value 12
Note: I am interested in semantic results, not simple pattern matching.