Capturing awk's system(cmd) output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Capturing awk's system(cmd) output
# 1  
Old 08-06-2010
Question 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
Code:
stat -c %s

into awk while supplying it with FILENAME?
Code:
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():

Code:
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:
Code:
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...
# 2  
Old 08-07-2010
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.

Code:
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.

Code:
# 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.
# 3  
Old 08-07-2010
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 --
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Invoking system(cmd) inside awk command

Hi, I was searching for a way to grep 2 lines before and after a certain keyword, and I came across the following code.. awk "\$0 ~ /ORA-/ { cmd=\"awk 'NR>=\" NR-2 \" && NR<=\" NR+2 \"' init.ora\" system(cmd) }" input_file I could not understand how this works. What is system() ? what... (2 Replies)
Discussion started by: Kulasekar
2 Replies

2. Programming

Problem on capturing system Shutdown

I am having exactly the same problem with https://www.unix.com/programming/129264-application-cleanup-during-linux-shutdown.html but the thread is old and closed. The only difference is that I use sigaction() instead of signal(), which is recommended, as far as I know. This is my code: ... (9 Replies)
Discussion started by: hakermania
9 Replies

3. Shell Programming and Scripting

awk question : system output to awk variable.

Hi Experts, I am trying to get system output to capture inside awk , but not working: Please advise if this is possible : I am trying something like this but not working, the output is coming wrong: echo "" | awk '{d=system ("date") ; print "Current date is:" , d }' Thanks, (5 Replies)
Discussion started by: rveri
5 Replies

4. Shell Programming and Scripting

Capturing Output?

Hello All, I'm writing a Bash Script and in it I execute a piped command within a Function I wrote and I can't seem to redirect the stderr from the 1st pipe to stdout..? I'm setting the output to an Array "COMMAND_OUTPUT" and splitting on newlines using this --> "( $(...) )". By putting... (6 Replies)
Discussion started by: mrm5102
6 Replies

5. Shell Programming and Scripting

system () output into file in awk

hello, I want to print my output into a file inside of awk, but I don't know it could wokr with using system (piping the $1-4 to another shellskript): cat file.txt |awk '{ if ($5==2) {dataname=$1 "_" $2 "_" $3 "_" $4 "_typing.rad" befehl=".gen_test " $7 " " $8 " " $8 system(befehl) >... (5 Replies)
Discussion started by: ergy1983
5 Replies

6. Shell Programming and Scripting

Capturing the output of dbv

Hello, We have an oracle database running on a Linux host (RHEL5)...I'm trying to run Oracle dbv (database verify utility) and capture its output to a file using the following syntax but the standart output does NOT get redirected to the file... dbv blocksize=32768 ... (2 Replies)
Discussion started by: luft
2 Replies

7. Shell Programming and Scripting

capturing output from top and format output

Hi all, I'd like to capture the output from the 'top' command to monitor my CPU and Mem utilisation.Currently my command isecho date `top -b -n1 | grep -e Cpu -e Mem` I get the output in 3 separate lines.Tue Feb 24 15:00:03 Cpu(s): 3.4% us, 8.5% sy .. .. Mem: 1011480k total, 226928k used, ....... (4 Replies)
Discussion started by: new2ss
4 Replies

8. HP-UX

awk to output cmd result

I was wondering if it was possible to tell awk to print the output of a command in the print. .... | awk '{print $0}' I would like it to print the date right before $0, so something like (this doesn't work though) .... | awk '{print date $0}' (4 Replies)
Discussion started by: IMTheNachoMan
4 Replies

9. Programming

using sendmail in system(cmd)

In 'C' I am trying to use the sendmail to send a message that program completed successful or failed. The syntax is: sprintf(cmd,"/usr/sbin/sendmail roncayenne@ssss.org to: roncayenne@ssss.org from:root subject: PROGRAM SUCCESSFUL ."); system(cmd); But this is not working. Seems it does not... (3 Replies)
Discussion started by: roncayenne
3 Replies

10. Shell Programming and Scripting

capturing output in script

I have the following line in my script: $sftpcmd $rmthost <<COMMANDS>> $sftplog 2>&1 For some reason this is not capturing the errors from sftp, they go to the file attached to the cron entry ie mm hh dd MM * /myscript > cron.out any idea why? digital unix 4.0d (6 Replies)
Discussion started by: MizzGail
6 Replies
Login or Register to Ask a Question