1

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.

1
  • user2567544 - did my answer help at all? Did you go awol?
    – tink
    Commented May 14 at 22:53

1 Answer 1

1

There's an ugly hack that works for me™:

cat ./1/2/b/test.yml
mane: "My name"
surname: "Foo"
street_address:
  city: "My city"
  street: "My street"
  number: 1


find -type f -iname \*.yml -print0 | xargs -0 -i^ bash -c "yq .mane ^ | grep -vq null && echo ^"
./1/2/b/test.yml

find -type f -iname \*.yml -print0 | xargs -0 -i^ bash -c "yq .name ^ | grep -vq null && echo ^"
./test.yml
./1/2/a/test.yml


Note that I intentionally used mane in the cated file =} and both mane and name in the find examples.

If you have carets ^ in your output you may have to find a different character to represent the file name...

Of course, should your queried entity contain the term 'null' you're sh*t out of luck because yq doesn't have the concept of returning a meaningful exit-code, it always returns 0. (Otherwise the whole thing would have been much simpler).

You must log in to answer this question.

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