For Programmers: Free Programming Magazines  


Home > Archive > AWK > March 2008 > awk pipe and system call









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author awk pipe and system call
uwcssa@gmail.com

2008-03-28, 6:59 pm

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

It may gets buffered inside awk, is there a way to turn auto buffer
off?


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);

Any ideas? thanks a bunch
pk

2008-03-28, 6:59 pm

uwcssa@gmail.com wrote:

> However, the following never works:
>
> BEGIN{ myvar1=0;}
> { myvar2 =1;
> system("echo " $myvar1 $myvar2);
>
> Any ideas? thanks a bunch


- use myvar1 and myvar2, without the "$";
- put a space between the variables:

$ echo "blah" | awk 'BEGIN {myvar1=0}
{myvar2=1;system("echo "myvar1" "myvar2)}'
0 1


--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
Ed Morton

2008-03-28, 6:59 pm



On 3/28/2008 10:27 AM, pk wrote:
> uwcssa@gmail.com wrote:
>
>
>
>
> - use myvar1 and myvar2, without the "$";


Not if he's trying to get $0 and $1.

> - put a space between the variables:
>
> $ echo "blah" | awk 'BEGIN {myvar1=0}
> {myvar2=1;system("echo "myvar1" "myvar2)}'
> 0 1


He should still be seeing the concatenation of $0 and $1 with what he had
(though all he said was that it "never works" so maybe that IS what he's seeing
and it's just not what he expected).

Ed.


pk

2008-03-28, 6:59 pm

Ed Morton wrote:

>
> Not if he's trying to get $0 and $1.
>
> He should still be seeing the concatenation of $0 and $1 with what he had
> (though all he said was that it "never works" so maybe that IS what he's
> seeing and it's just not what he expected).


Ah yes, that's another possibility. Mine was just my interpretation of the
problem, and admittedly he did not provide too much context or details.

--
All the commands are tested with bash and GNU tools, so they may use
nonstandard features. I try to mention when something is nonstandard (if
I'm aware of that), but I may miss something. Corrections are welcome.
Ed Morton

2008-03-28, 6:59 pm



On 3/28/2008 9:59 AM, uwcssa@gmail.com 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
>
> It may gets buffered inside awk, is there a way to turn auto buffer
> off?


Take a look at this thread:

http://tinyurl.com/33ua89

Regards,

Ed.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com