[TriLUG] awk question - or something better if you have it
William Sutton via TriLUG
trilug at trilug.org
Tue Aug 18 13:27:54 EDT 2015
Also, IIRC, Perl's say() appends a newline, which you might not want...
William Sutton
On Tue, 18 Aug 2015, Brian Gerard via TriLUG wrote:
> 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
> --
> This message was sent to: William <william at trilug.org>
> To unsubscribe, send a blank message to trilug-leave at trilug.org from that address.
> TriLUG mailing list : http://www.trilug.org/mailman/listinfo/trilug
> Unsubscribe or edit options on the web : http://www.trilug.org/mailman/options/trilug/william%40trilug.org
> Welcome to TriLUG: http://trilug.org/welcome
More information about the TriLUG
mailing list