Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #14986

Re: Run AWK using shell_exec

From Matthew Carter <m@ahungry.com>
Newsgroups comp.lang.php
Subject Re: Run AWK using shell_exec
Date 2015-02-27 00:28 -0500
Organization Ahungry (http://ahungry.com)
Message-ID <87sidrrkh7.fsf@ahungry.com> (permalink)
References <bd5897d2-065d-4cf1-bd7f-cbcb3404cb25@googlegroups.com> <mcoev3$bt4$1@dont-email.me> <98db9ab8-dec1-4b3a-b790-285e967c64bb@googlegroups.com> <87egpcozxq.fsf@bsb.me.uk> <75f1d7f7-e73e-452c-acb6-07ac2c0fff0f@googlegroups.com>

Show all headers | View raw


"Dan'l B" <dan.hinckley@gmail.com> writes:

> On Thursday, February 26, 2015 at 9:23:19 PM UTC-5, Ben Bacarisse wrote:
>> "Dan'l B" <> writes:
>> <snip>
>> > 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:

<?php

$data = array_filter (array_map (
  function ($l)
  {
    return explode (' ', preg_replace ('/\s+/', ' ', $l))[3];
  }, explode ("\n", file_get_contents ('/tmp/hi-low'))), 'is_numeric');

sort ($data); 

$d1 = $d2 = $data; // This is done incase there is only one entry

$low = array_shift ($d1);
$hi  = array_pop ($d2);

var_dump ($low, $hi);

Fine tune as needed of course (this assumes one or more whitespace such
as tab or space breaks the 'columns').  You'd probably want your data to
stick to a real column format, such as CSV or TSV though.

-- 
Matthew Carter (m@ahungry.com)
http://ahungry.com

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Run AWK using shell_exec "Dan'l B" <dan.hinckley@gmail.com> - 2015-02-26 14:40 -0800
  Re: Run AWK using shell_exec Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-26 19:50 -0500
    Re: Run AWK using shell_exec "Dan'l B" <dan.hinckley@gmail.com> - 2015-02-26 17:21 -0800
    Re: Run AWK using shell_exec "Dan'l B" <dan.hinckley@gmail.com> - 2015-02-26 17:41 -0800
      Re: Run AWK using shell_exec Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-26 20:51 -0500
      Re: Run AWK using shell_exec Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-02-27 02:23 +0000
        Re: Run AWK using shell_exec "Dan'l B" <dan.hinckley@gmail.com> - 2015-02-26 19:11 -0800
          Re: Run AWK using shell_exec Matthew Carter <m@ahungry.com> - 2015-02-27 00:28 -0500
          Re: Run AWK using shell_exec Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-02-27 11:14 +0000
        Re: Run AWK using shell_exec Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2015-02-27 14:17 +0100
          Re: Run AWK using shell_exec "Dan'l B" <dan.hinckley@gmail.com> - 2015-02-27 11:40 -0800
            Re: Run AWK using shell_exec Jerry Stuckle <jstucklex@attglobal.net> - 2015-02-27 16:03 -0500

csiph-web