I want to run awk from php to do some text processing. I am giving an extremely simple example below:

onecol.awk file

{
print "Hello!";
}

f1.txt

aaa
ccc
eee

f2.txt

ddd
eee
fff


index.php

<?php
$command1 ="awk -f onecol.awk f1.txt > outsingle.txt";
exec($command1); 

$command2 ="join <(awk -f onecol.awk f1.txt) <(awk -f onecol.awk f2.txt) > outdouble.txt";
exec($command2);
?>

In this php, first exec command works while the second one does not work. Why is it so?
There should not be any permission problem as the first exec command is creating the outsingle.txt file.

Both the commands work perfectly on console.

Thanks

    Have you tried using exec()'s third parameter to capture the return status code to see if it points to a specific error? Have you tried using something like [man]shell_exec/man instead and examining the output to see if an error message is being printed that might help? Are the directives display_errors set to On and error_reporting set to E_ALL? If so, are you getting any PHP error messages?

      Hi bradgrafelman
      Thanks for reply

      <?php
      
      $command1 ="awk -f agg.awk f1.txt > outsingle.txt"; 
      system($command1); 
      
      $command2 ="join <(awk -f agg.awk f1.txt) <(awk -f onecol.awk f2.txt) > outdouble.txt"; 
      system($command2); 
      
      ?>
      

      The shell command works perfect.
      When I run the php file using shell
      I get the following error

      $php myfile.php
      sh: Syntax error: "(" unexpected

        That output would suggest that the shell used was the 'Bourne Shell' (usually at: /bin/sh). I'm betting you were using the 'Bourne Again SHell' (usually at: /bin/bash). You can confirm this by logging in and executing this:

        echo $SHELL

        from the terminal. You'll get '/bin/bash' (or some other prefix before '/bash') if I'm right.

        Now, I'm no *nix shell expert, but I'd guess that if the command works just fine for you from bash yet sh is complaining about it, then the syntax must be different (and/or simply not valid) for the latter.

        EDIT: Okay, I said I'm no expert, so can you explain to me why you're doing this:

        join <(awk -f onecol.awk f1.txt) <(awk -f onecol.awk f2.txt) > outdouble.txt

        rather than just this:

        awk -f onecol.awk f1.txt f2.txt > outdouble.txt

        ?

          Hi bradgrafelman
          Thanks for reply.
          You were right. The command line that I was using uses 'bash' where as php uses 'sh'. To solve the problem, somehow i need to instruct php to select bash and not sh.

          Is there any setting in php or anywhere else so that i can instruct php to only execute bash?

          I have n files, and I want to perform some processing based on instruction in onecol.awk and then combine all these n files together to get one comsolidated file.
          If I can execute this command, I can execute rest of the commands also. I am hoping that this script willl give me a very good speed as mose of my data files are really huge with tens of thousands of rows.

          join <(awk -f onecol.awk f1.txt) <(awk -f onecol.awk f2.txt) > outdouble.txt

            Note on the man page for bash that it offers a variety of ways to invoke it, such as by using the -c option to simply execute a string in the context of the bash shell.

              bradgrafelman;10995025 wrote:

              Note on the man page for bash that it offers a variety of ways to invoke it, such as by using the -c option to simply execute a string in the context of the bash shell.

              Can we do similar setting in php also?

                That's what I was referring to; alter your PHP system call to instead invoke the bash interpreter and use the -c option to pass in the command you've been trying to execute.

                  bradgrafelman;10995027 wrote:

                  That's what I was referring to; alter your PHP system call to instead invoke the bash interpreter and use the -c option to pass in the command you've been trying to execute.

                  Its working now.
                  Thanks a lot bradgrafelman.

                  $command ="join -t$'\t' -a1 -a2 -e- -j1 -o0,1.2,2.2 <(awk -f agg.awk f1.txt) <(awk -f agg.awk f2.txt) > outsecond.txt ";
                  shell_exec("/bin/bash -c \"$command\"");

                    Write a Reply...