# Get the USB port 'by path' to autofill in udev rules file

I'm trying to improve on my udev rules for USB serial devices or USB sound cards. I want the USB port number to be populated in the ENV{ID_PATH} field without having to edit it myself (this becomes useful if I'm copying this rules file to a different PC for example). I know that I can write a bash script to parse the result of (in case of serial devices)

udevadm info /dev/ttyUSB0 | grep "ID_PATH="


or by parsing the result of (in case of sound devices)

udevadm monitor --kernel --subsystem-match=sound


to get the value and write it to the rules file.

But I got more interested in the RUN and PROGRAM fields. I did come across examples like this and this where an external service/script is called, but none write back to any field within the rules file itself. I'm wondering if it is even possible that I can run a script and fill the value of the key ENV{ID_PATH} with the sysfs path (like a self-configuring or self-learning rules file)? Does udev provide such a functionality? If yes, how can I do it?