Link to home
Start Free TrialLog in
Avatar of Chris-M
Chris-M

asked on

Awk and its System command

Hi!

Right, I am trying to get an awk script going that uses the system command.
The first line of an input file (called inputfile) is (without quotes) "This is a text file"

The awk script (called awkscript) is...

#! /bin/awk -f

BEGIN{FS=" "
print "Print Command Output"
system("Echo System command in BEGIN")
}
{
print $1, $2, $3, system("echo System command in pattern Section")
}


Running the script as such, # awkscript inputfile > outputfile and cat'ing the outpulfile shows...

Print Command Output
System command in BEGIN
System command in pattern Section
This is a 0

I was expecting...

Print Command Output
System command in BEGIN
This is a System command in pattern Section

As you can see, the system command works as expected in the BEGIN part, but doesnt in the pattern section, well it doesn't for GNU Awk 3.1.0, anyone know why/a workaround?

Thanks -Chris
Avatar of Chris-M
Chris-M

ASKER

Hmm, just read..

"note that the system function works in nawk only." I may have to try that
Well I guess the script is printing the result of the system command
since the command is successfully executed, the return is 0 which is gettig printed.
But I wonder what happens to the echo in the system command.
It is getting echoed - didnt notice it earlier ;-)
print $1, $2, $3, " System command in pattern Section"
Avatar of Chris-M

ASKER

That was just an example (a bad one, it would seem!), I'll explain better.

the system command executes what you tell it to. When run in the BEGIN section it returns what the executed command would return (e.g. system(echo hello!) returns hello!, system(pwd) would return /usr/sbin/ [where the script is run from]).

However, when run from the pattern section, it returns the errorcode (which is 0 becase the command ran successfully, but prints the actual output first...

I really need to make the output from a system command part of the print line. To be honest though, I do not think the system command is supposed to return the output, just its error code, so is there an alternative?
AFAIK there is no way to catch STDERR and/or STDIN from system() within awk or nawk
use perl instead
Avatar of Chris-M

ASKER

Yeah, I think so too, its just that it appeared to work in the BEGIN section.

In the end I did come up with quite a long winded way of getting what I need, done. By running the system command before the print line. This means I get 2 lines, the first containing the system output, the second with the original information.

I then process this new text file again with a slightly different awk program that pretty much joins the 2 lines together in the way I need. Its not pretty, but it works. However, I will definately consider perl for any future scripting I do!
another awful aproach would be to start your system command like this:
  system("you-prog >/tmp/log 2>&1"); # assuming that system() uses /bin/sh
and then open and read /tmp/log from within your awk program right after system() finished
hmm, so everybody will ask 500 pts questions in future, collect the suggestions and then lower the points?
Think this is not how EE works.
Avatar of Chris-M

ASKER

That wasn't my intention.

I requested that I get 100 points back (for answering the question myself). And you get 25 for your troubles.
Chris-M, AnnieMod,
it's not about the points (for me), but about giving back points i.g.

AnnieMod, I suggest a 0 pts PAQ, refund. Then mark the last 3 and this comment as "visible for moderators" only, ask netminder how to do that.
Also I'd suggest to put the "lower points" on the discussion list for EE administrators and moderators
ASKER CERTIFIED SOLUTION
Avatar of Computer101
Computer101
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial