[TriLUG] awk question - or something better if you have it

Dewey Hylton via TriLUG trilug at trilug.org
Tue Aug 18 13:03:03 EDT 2015


for purposes of portability, i'm trying to dynamically determine the correct location of the config file for the currently-installed nginx binary. as per usual, this was a single little bullet on my list and is taking longer than expected.

so far this works on the versions i've tested of linux/freebsd/openbsd, though it fails on illumos (sunos) due to difference in grep:
    nginx -V 2>&1|egrep -oe '--conf-path=[^ ]*'|awk -F= '{ print $2 }'

this one seems to work in all those environments, except that it prints all the lines:
    nginx -V 2>&1 | sed -E 's/.*--conf-path=([^ ]*).*/\1/'

obviously i could couple this with another grep or something, but ideally i'd like to be able to use a single command. i originally thought awk would be simple but i'm unsure how to match a string and have awk only return the matching column/field without writing quite a bit more than a one-liner.

so ... i figured i'd ask here if there may be another way i hadn't thought of, or a simple tweak to one of the above methods.


More information about the TriLUG mailing list