Talk About Network



Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Awk > Re: awk pipe an...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 7 Topic 2208 of 2236
Post > Topic >>

Re: awk pipe and system call

by Janis Papanagnou <Janis_Papanagnou@[EMAIL PROTECTED] > Apr 27, 2008 at 12:06 PM

privat@[EMAIL PROTECTED]
 wrote:
> On 28 Mrz., 16:59, "uwc...@[EMAIL PROTECTED]
" <uwc...@[EMAIL PROTECTED]
> wrote:
> 
>>i have two problems with awk:
>>
>>first, the following works on my system, which shows system usage
>>
>>    iostat 2 | awk '{print $1}'
>>
>>However, the following seems never gets written to the log file
>>
>>       iostat 2 | awk '{print $1}'  > log

(That's a shell issue and off-topic; the reason is the buffering
that is done in case that the output device is no tty. There are
various possibilities to disable buffering (or rather to do line
buffering); on my system, e.g., I can use a program called pty...

   iostat 2 | pty awk '{print $1}' >log

>>
> 
> Try this instead:
> 
> iostat 2|awk '{system("echo " $1 " >>log")}'
> 
> (tested and works)
> 
> 
>>Second problem is that I found it is impossible to send variables to
>>system calls:
>>the following works inside awk script:
>>
>>       system( "echo " $0);
>>
>>However, the following never works:
>>
>>      BEGIN{ myvar1=0;}
>>      { myvar2 =1;
>>        system("echo " $myvar1  $myvar2);

Generally I'd try to avoid using system(); if you just want to
print the variables use:  print myvar1, myvar2  (Also mind that
in shell echo is an unrealiable, non-portable command.)

>>
>>Any ideas? thanks a bunch

Since myvar2 is 0 and myvar2 is 1 awk will see the system() call as

         system("echo " $0  $1);

which might not be what you expect.

> 
> 
> awk doesn't support "$" before variable names. Try the following:

It *does* support "$" before variable names, but it has a different
meaning than a shell programmer might expect.

Janis

> 
> system("echo " myvar1 myvar2)
> 
> (tested and works)
> 
> Have a nice day
> Robert




 7 Posts in Topic:
awk pipe and system call
"uwcssa@[EMAIL PROTE  2008-03-28 07:59:31 
Re: awk pipe and system call
pk <pk@[EMAIL PROTECTE  2008-03-28 16:27:36 
Re: awk pipe and system call
Ed Morton <morton@[EMA  2008-03-28 10:15:45 
Re: awk pipe and system call
pk <pk@[EMAIL PROTECTE  2008-03-28 16:40:43 
Re: awk pipe and system call
Ed Morton <morton@[EMA  2008-03-28 10:42:12 
Re: awk pipe and system call
privat@[EMAIL PROTECTED]   2008-04-25 10:23:19 
Re: awk pipe and system call
Janis Papanagnou <Jani  2008-04-27 12:06:46 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu May 15 22:40:30 CDT 2008.