views:

570

answers:

2

I need to execute a command per line of some file. For example:

file1.txt 100 4
file2.txt 19 8

So my awk script need to execute something like

command $1 $2 $3

and save the output of command $1 $2 $3, so system() will not work and neither will getline. (I can't pipe the output if I do something like this.)

The restriction to this problem is to use only awk. (i already had a solution with bashscriot + awk...but I only want awk...just to know more about this)

A: 

What's wrong with using getline?

$ ./test.awk test.txt
# ls -F | grep test
test.awk*
test.txt

# cat test.txt | nl
     1  ls -F | grep test
     2  cat test.txt | nl
     3  cat test.awk

# cat test.awk
#!/usr/bin/awk -f
{
        cmd[NR] = $0
        while ($0 | getline line) output[NR] = output[NR] line RS
}
END {
        for (i in cmd) print "# " cmd[i] ORS output[i]
}
ephemient
this is bash script. when I use getline I cant pipe the command, for examplecat file | process parameters, I only gets the cat file.I am doing this for more clean ways to do this task...
llazzaro
Which `awk`? It works here, with GNU Awk 3.1.7.
ephemient
the last 10 lines answered my question
llazzaro
Hmm, I thought it was obvious that this was a demonstration of the script running on itself, but maybe it wasn't.
ephemient
A: 
Peter Cordes
my actual solution is doing this, but I cant send the output of command to a variable
llazzaro
You could slurp the output back in with a subsequent `readline [...] < $1 ".out"` loop. Don't forget to clean up after yourself...
ephemient
I used some file-descriptor redirection to pipe and getline at the same time. Does that do what you need?
Peter Cordes