I've been sturggling to figure out how to use jq
to map a list of files to a given JSON structure. I have the following list of files:
/tmp/jboss/ejb-jar_3_2.xsd
/tmp/jboss/example.crt
/tmp/jboss/example.key
/tmp/jboss/incomingrunasidentityloginmodule.zip
/tmp/jboss/jboss-eap-7.0.0.zip
/tmp/jboss/jboss-eap-7.0.8-patch.zip
/tmp/jboss/keystore.jks
/tmp/jboss/logger-api.sh
/tmp/jboss/mgmt-api-example.txt
/tmp/jboss/wildfly-dist-10.1.0.Final.zip
/tmp/jboss/wildfly-dist-7.1.0.GA-redhat-8.jar
that I would like to convert to the following JSON structure, where the dirname is the final path in the directory structure.
[
{ dirname: "jboss", filename: "/tmp/jboss/ejb-jar_3_2.xsd" },
{ dirname: "jboss", filename: "/tmp/jboss/example.crt" },
...
...
]
I figure the regex I can use to extract the path is fairly simple: .*/(.*)/
or something close to that. However, I can't figure out how to structure the jq
filter. I figure it needs to be something like:
cat /tmp/files.txt | jq --raw-input '. | { dirname: .match(regex).capture[0], filename: .}'
is as close as I've gotten, but that has several issues. 1) I can''t figure out how to use a regex for the dirname, and 2) this doesn't actually build the list of objects - it just generates multiple objects.
Is there an easy jq
filter that I can use for this?
jq -nR '
[inputs | capture(".*/(?<dirname>[^/]*)/[^/]+$") + { filname: .}]'