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

Kevin Otte via TriLUG trilug at trilug.org
Tue Aug 18 13:30:19 EDT 2015


This what you mean?

kjotte at mystic:~$ nginx -V 2>&1 | awk 'BEGIN {RS=" ";FS="=";} $1 ~
/conf-path/{print $2}'
/etc/nginx/nginx.conf

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.
> 


More information about the TriLUG mailing list