Sponsored Content
Top Forums Shell Programming and Scripting Capturing awk's system(cmd) output Post 302443139 by agama on Friday 6th of August 2010 11:11:21 PM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

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

8. 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

9. 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

10. 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
code(n) 							    [incr Tcl]								   code(n)

__________________________________________________________________________________________________________________________________________________

NAME
code - capture the namespace context for a code fragment SYNOPSIS
itcl::code ?-namespace name? command ?arg arg ...? _________________________________________________________________ DESCRIPTION
Creates a scoped value for the specified command and its associated arg arguments. A scoped value is a list with three elements: the "@scope" keyword, a namespace context, and a value string. For example, the command namespace foo { code puts "Hello World!" } produces the scoped value: @scope ::foo {puts {Hello World!}} Note that the code command captures the current namespace context. If the -namespace flag is specified, then the current context is ignored, and the name string is used as the namespace context. Extensions like Tk execute ordinary code fragments in the global namespace. A scoped value captures a code fragment together with its namespace context in a way that allows it to be executed properly later. It is needed, for example, to wrap up code fragments when a Tk widget is used within a namespace: namespace foo { private proc report {mesg} { puts "click: $mesg" } button .b1 -text "Push Me" -command [code report "Hello World!"] pack .b1 } The code fragment associated with button .b1 only makes sense in the context of namespace "foo". Furthermore, the "report" procedure is private, and can only be accessed within that namespace. The code command wraps up the code fragment in a way that allows it to be exe- cuted properly when the button is pressed. Also, note that the code command preserves the integrity of arguments on the command line. This makes it a natural replacement for the list command, which is often used to format Tcl code fragments. In other words, instead of using the list command like this: after 1000 [list puts "Hello $name!"] use the code command like this: after 1000 [code puts "Hello $name!"] This not only formats the command correctly, but also captures its namespace context. Scoped commands can be invoked like ordinary code fragments, with or without the eval command. For example, the following statements work properly: set cmd {@scope ::foo .b1} $cmd configure -background red set opts {-bg blue -fg white} eval $cmd configure $opts Note that scoped commands by-pass the usual protection mechanisms; the command: @scope ::foo {report {Hello World!}} can be used to access the "foo::report" proc from any namespace context, even though it is private. KEYWORDS
scope, callback, namespace, public, protected, private itcl 3.0 code(n)
All times are GMT -4. The time now is 06:40 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy