[NZLUG] udev shell (or awk or ...) one-liner puzzle

Jim Cheetham jim at gonzul.net
Fri Jul 8 22:58:37 NZST 2016


On Fri, Jul 8, 2016 at 8:06 PM, Eliot Blennerhassett <ewblen at gmail.com> wrote:
> pci-0000_00_1a_0-usb-0_1_2_4_3_1_3

Well, here's an ugly use of sed (twice) and dc, trying to avoid "
characters (which could probably be quoted with \" anyway, but never
mind)

sed -re 's/.*_(.)_._(.)$/\1 2 - 4 * \2 + p/' | dc | sed -re
's/^(.*)/VSCOM_PORT_IDX=\1/'

The first sed finds the "third from last" and the last thing (single
character) that is _-separated in the input, and stuffs them into a
sum for dc; then a second sed takes that output and pops the fixed
text you wanted in front.

Another option would be pure bash ...

bash -c "if [[ $env{ID_PATH_TAG} =~ (.)_._(.)$ ]]; then echo
VCSOM_PORT_IDX=$(( (${BASH_REMATCH[1]} - 2) * 4 +
${BASH_REMATCH[2]})); fi"

This relies on the =~ command populating the BASH_REMATCH array, so
you get the substrings for free. But sadly this'll require you to
quote the "" chars, which might be difficult for you.

-jim


More information about the NZLUG mailing list