calling awk from php not working


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting calling awk from php not working
# 1  
Old 01-19-2012
calling awk from php not working

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
# 2  
Old 01-19-2012
exec replaces your program. Once you've done exec, your program no longer exists!

Try system().
# 3  
Old 01-19-2012
I tried system() command also but not working .
# 4  
Old 01-19-2012
In what way is system() not working?

---------- Post updated at 12:10 PM ---------- Previous update was at 12:04 PM ----------

You can't redirect two files into one program. Why not feed both files into one awk?

Code:
awk -f whatever file1 file2 > outdouble

# 5  
Old 01-19-2012
I have variable number of files and I want to perform aggregate on each file and then combine. The related thread by me is on the following address.
https://www.unix.com/unix-advanced-ex...-into-one.html

That is why I wrote a code that create awk command for n number of files.

But I am stuck at this point as awk is not being executed when called from php.
Thanks
# 6  
Old 01-19-2012
Code:
$command1 ="awk -f onecol.awk f1.txt > outsingle.txt";

I don't know if it is good form, but I do this
Code:
$command1 = `awk -f onecol.awk f1.txt`;

Then $command1 contains the output.
# 7  
Old 01-19-2012
Thanks for reply.

I wrote a php code that creates the following command:

join -t$'\t' -a1 -a2 -e- -j1 -o0,1.2,2.2 <(awk -f agg.awk fff1.txt) <(awk -f agg.awk fff2.txt) |
join -t$'\t' -a1 -a2 -e- -j1 -o0,1.2,1.3,2.2 - <(awk -f agg.awk fff3.txt) |
join -t$'\t' -a1 -a2 -e- -j1 -o0,1.2,1.3,1.4,2.2 - <(awk -f agg.awk fff4.txt) |
join -t$'\t' -a1 -a2 -e- -j1 -o0,1.2,1.3,1.4,1.5,2.2 - <(awk -f agg.awk fff4.txt) |
join -t$'\t' -a1 -a2 -e- -j1 -o0,1.2,1.3,1.4,1.5,1.6,2.2 - <(awk -f agg.awk fff6.txt) > tmp.txt

My only concern now is why it does not work if I call from php.

My php file is:

PHP Code:
<?php
$command 
="awk -f onecol.awk f1.txt > outfirst.txt";
system($command);

$command ="join -t$'\\t' -a1 -a2 -e- -j1 -o0,1.2,2.2 <(awk -f agg.awk fff1.txt)     <(awk -f agg.awk fff2.txt)  >  outsecond.txt ";
system($command);
?>
Thanks

Last edited by mary271; 01-19-2012 at 03:19 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PHP cronjob not working but manual working

Hi, Can anyone help me on my PHP cron not working, but when i do the manual it work. # manual run working /usr/local/bin/php /root/dev/test.php # crontab not working 55 8 * * * /usr/local/bin/php /root/dev/test.php Thank in advances Regards, FSPalero Please use CODE tags as... (2 Replies)
Discussion started by: fspalero
2 Replies

2. UNIX for Advanced & Expert Users

Awk expressions working & not working

Hi, Putting across a few awk expressions. Apart from the last, all of them are working. echo a/b/c | awk -F'/b/c$' '{print $1}' a echo a/b/c++ | awk -F'/b/c++' '{print $1}' a echo a/b/c++ | awk -F'/b/c++$' '{print $1}' a/b/c++ Request thoughts on why putting a '$' post double ++... (12 Replies)
Discussion started by: vibhor_agarwali
12 Replies

3. Programming

Calling macro in shell_exec not working

Hi guys! I really need your help. I have a php code that should convert doc, ppt,etc. to pdf using openoffice. But its not working, and im not sure what the problem is. Here's my php code: define('OOFFICE_LIBRARY', '/usr/lib/openoffice.org/program/'); $convertToPdf = OOFFICE_LIBRARY .... (5 Replies)
Discussion started by: tweine
5 Replies

4. Shell Programming and Scripting

ssh is not working while calling through expect shell script

Hi, Please share you experience and way out on below error:--> #!/bin/bash -xv FILE=login.txt + FILE=login.txt CONNECT=sshlogin.exp + CONNECT=sshlogin.exp SERVERNAME=$1 + SERVERNAME=192.168.12.1 MyServer="" + MyServer= MyUser="" + MyUser= MyPassword="" + MyPassword= exec 3<&0 +... (6 Replies)
Discussion started by: manish_1678
6 Replies

5. Shell Programming and Scripting

Calling php from shell script

Maybe someone here can help out. I have a script cat <somefile> | while read hostname do /usr/bin/php <some php script> $hostname done This script works great for the first entry in the host list. However it will only execute and ignores the loop part. It's almost as if it loses... (4 Replies)
Discussion started by: dbakyle
4 Replies

6. Shell Programming and Scripting

Calling an Expect script from a Webpage using PHP

I have a webpage that is in HTML and PHP. In PHP I have tried using exec, system, shell_exec and passthru functions to call an Expect Script file (temp.exp). This Expect file spawns a telnet session that uses "expect/send" commands to retrieve information from an environmental unit (not a normal... (0 Replies)
Discussion started by: CCUSmith
0 Replies

7. Shell Programming and Scripting

Calling a shell script from php

Hi, I have a shell script and I'm trying to execute it from my php script. The command I'm using is: shell_exec("Script.sh"); Is this correct? It seems to not do anything for me. Not sure if this might be a permission issue or not. I have both scripts 777 permissions. Maybe I got the... (1 Reply)
Discussion started by: eltinator
1 Replies

8. Shell Programming and Scripting

calling current working dir from script

Hello, I am having problem in setting current working directory from shell. I want to set pwd as an environmental variable in a script. I am following an existing script which is defined as HOME=$(shell dirname `pwd`) C_HOME=$(shell echo $(HOME) | sed -e 's:\/:\\\/:g' ) But when I am trying... (3 Replies)
Discussion started by: chandra004
3 Replies

9. UNIX for Dummies Questions & Answers

calling mkdir from PHP

Edit: From a post in this topic, found out that my problem wasn't what I thought it was, and ended up solving my problem in aonther topic: https://www.unix.com/showthread.php?p=302076761#post302076761 Hi, I'm not sure if this is a Unix problem or a PHP problem, but I'll see if anyone here... (4 Replies)
Discussion started by: gmclean2006
4 Replies

10. UNIX for Dummies Questions & Answers

Calling a PHP script from cron

This is a line from my crontab: 12 12 * * * /home/users/ElburdNDL/www/backups/adddate.php The permissions of the script is 755 it should execute ok....but it doesn't. Do I somehow have to give cron the path to PHP or something? If so, how exactly? Thanks. Ed PS Am a total newbie... (10 Replies)
Discussion started by: eludlow
10 Replies
Login or Register to Ask a Question