Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Interaction between awk and a private gcc-compiled program 1

Status
Not open for further replies.

sibawe

Technical User
Dec 21, 2003
3
FR
Hello,

I need to interact awk with an existing program (gcc-compiled) I wrote. This gcc-program takes arguments at the command line and returns a number.
More precisely, the arguments are listed in a text file ; I need awk to fetch the arguments, serve them as input to the gcc-program and , ideally, add the result to the text file in a new column.

The following example is deliberately simple. My actual gcc-program is more complicated and can't be converted to awk.


[root]# cat gcc_program.c

#include <stdio.h>

int main (int argc, char *argv[])
{
int i,j,sum;
i=atoi(argv[1]); /* cast argv[1] to integer i */
j=atoi(argv[2]); /* cast argv[2] to integer j */
sum=i+j; /* add both */

printf ("%d",sum); /*print sum on stdout */

return (0);
}

[root]#cat input.txt
2 4
4 8
1 4

I tried the following (after adding the current directory in PATH):

[root]# awk '{sum = gcc_program $1 $2} {print sum}' input.txt
24
48
14

It basically concatenates both arguments.

Does anyone have an idea? Thanks!!

Sibawe
 

awk '{"gcc_program " $1 " " $2 | getline sum; print $0,sum}' input.txt >out.txt

 
Your answer works fine.
Thx futurelet!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top