[TriLUG] awk question - or something better if you have it
Brian Gerard via TriLUG
trilug at trilug.org
Tue Aug 18 13:12:34 EDT 2015
On 08/18/2015 01:03 PM, Dewey Hylton via TriLUG wrote:
>
> 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.
At this point, I'd whip out the Swiss Army Chainsaw(tm) of Perl:
nginx -V 2>&1 | perl -ne 'next unless /--conf-path=(\S+)/; print "$1\n"'
...and I know in more recent Perls you can use 'say' instead of that print, but
portability was mentioned, so I didn't want to assume. :)
HTH-
Brian
More information about the TriLUG
mailing list