0

I would like to find a way to get the unique keys that are used inside .rb and .yml files. These keys are used inside the hash ENV. So, there are files that contain things like:

ENV['key']

in multiple lines and at various positions within their lines. Also, those might appear multiple times within those lines.

So, If I have the file:

blah blah blah ENV['key1'] blah blah blah ENV['key2']
blah blah blah ENV['key2']
blah blah
ENV['key3'] blah blah ENV['key2']

then I would like to get the list:

key1
key2
key3

I have managed to do this:

find . -iname "*.rb" -o -iname "*.yml" | xargs egrep 'ENV\[.(.*).\]'

but I do not know how to actually get the keys.

Any help would be much appreciated.

1 Answers1

0

Try using the -o flag to grep to get only the matching parts of the line:

find . '(' -iname "*.rb" -o -iname "*.yml" ')' -exec grep -o -E 'ENV\[.(.*).\]' {} + | sort -u | cut -d'[' -f 2 | tr -d "']"

Also, I highly recommend using the -exec flag to find instead of piping to xargs:

jayhendren
  • 8,384
  • 2
  • 33
  • 58