Capturing awk's system(cmd) output

Hi everybody,

I am working on a bigger awk script in which one part is comparing the size of two files.
I want to evaluate which file is bigger and then just save the bigger one.
I got it all working except for the part where I want to figure out which file is bigger; the one awk is currently reading (->FILENAME) or the one that already lies there.
So how do you get the output of

stat -c %s

into awk while supplying it with FILENAME?

I tried it with getline:
awk '{\                                                                                                                                                                                                                                     
    if (FNR ==1) {\                                                                                                                                                                                                                         
        "stat -c %s "FILENAME |getline fsize\
        if (fsize <= ....){\
        ...}\                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                              
    }\                                                                                                                                                                                                                                      
}' /export/hundreds/of/files/*

But that ends up in <Permission Denied> (Don't ask me why, I am the owner of the script as well as the files to be read)

Another approach was system():

awk '{\                                                                                                                                                                                                                                     
if (FNR == 1) {\                                                                                                                                                                                                                                                                                                                                                                                                                                                     
    fsize = system("stat -c %s " FILENAME)\                                                                                                                                                                                         
      if (fsize <= ....){\
      ...}\
    }\                                                                                                                                                                                                                                                                                                                                                                                                                                                                            
}' /export/hundreds/of/files/*

But fsize ends up being 0, since its just the status of the cmd, or something like that.

Next approach I found online involved something like this:

x="'"`stat -c %s FILENAME`"'"

But that obviously can't work because FILENAME is an awk variable. And I don't see a way of putting FILENAME in that command.

It would be really great if someone has some idea on how to solve the problem. I have been sitting on this problem for a couple of hours now and I am starting to go nuts...

The system command will not work for you because all that awk makes available is the return code which will be 0 for success or !0 for failure. You could use system and have the command output redirected to a file, then read the file, but that's a lot of unnecessary work.

Your attempt at getline seems flawed only in that you've escaped the newline without terminating the statement with a semicolon. The following works for me with GNU Awk 3.1.6.

awk '
        FNR==1 {
                c = "stat -c %s " FILENAME;
                c |getline foo;
                print ">>" foo;
                close( c );
        }

' t7.ksh


You also need to remember to close the "command" after you are done. The close must be passed the exact command string that you started the pipe with which is why I usually put it into a variable, or write a standalone function (example below) to deal with external commands.

# simple script that reads a commands from stdin and executes them
# prints all output from the command, not just the first line. 
awk '
        function cmd( c )
        {
                while( (c|getline foo) > 0 )
                        printf( ">>%s\n", foo );
                close( c );
        }

        {
                cmd( $0 );
        }
'

Hope this gives you enough to go on.

Thank you very much agama!

I am using the backslashes because I have to run it on a csh, sorry I forgot to mention that.

But the part of first assigning it to a variable and then running getline on that variable works perfectly!

Also thanks for the script!

-- problem solved --