Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Matthew Carter Newsgroups: comp.lang.php Subject: Re: Run AWK using shell_exec Date: Fri, 27 Feb 2015 00:28:52 -0500 Organization: Ahungry (http://ahungry.com) Lines: 58 Message-ID: <87sidrrkh7.fsf@ahungry.com> References: <98db9ab8-dec1-4b3a-b790-285e967c64bb@googlegroups.com> <87egpcozxq.fsf@bsb.me.uk> <75f1d7f7-e73e-452c-acb6-07ac2c0fff0f@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="7c986cd4736462de309a749b207746fe"; logging-data="20235"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+K+T82dHWdyRfKQXg7S9R2" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux) Cancel-Lock: sha1:Z0abzYcSgzOj/dN8FeSk/t4EOSc= sha1:3/CJf8DG1nJCa0j3BIphDbvYqZA= Xref: csiph.com comp.lang.php:14986 "Dan'l B" writes: > On Thursday, February 26, 2015 at 9:23:19 PM UTC-5, Ben Bacarisse wrote: >> "Dan'l B" <> writes: >> >> > Substituting a variable containing a path-to-the file, I can't seem to >> > get the escaping right: >> > >> > Works: >> > $maxLow = shell_exec("/usr/bin/awk 'min==\"\" || $4~/^[^[:alpha:]]+$/ >> > && $4 < min {min=$4} END{print min}' fullyear.txt"); >> > >> > Fails: (where $tidesFilePath is an absolute path to the fullyear.txt file). >> > $tidesFilePath = "/Tidefiles/fullyear.txt"; (example path) >> > $maxLow = shell_exec("/usr/bin/awk 'min==\"\" || $4~/^[^[:alpha:]]+$/ >> > && $4 < min {min=$4} END{print min}'" . $tidesFilePath . \""); >> >> That's not syntactically correct PHP. I'm not sure what the . \"" bit >> at the end is supposed to do, but you don't need it. You *do* need a >> space between the end of the awk script in single quotes and the file >> name: >> >> ... $4 < min {min=$4} END{print min}' " . $tidesFilePath); >> >> But you should be very careful when including variables into a shell >> command. Never forget little Bobby Tables http://www.xkcd.com/327/ >> > Thanks, still doesn't seem to work but I'll double-check the path. I > do grasp the problem of variables in shell commands but is there an > alternative way to get the highest and lowest values in a column of > text in a multi-column text file using only PHP? This works on your sample data: