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
Chris-MAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Chris-MAuthor Commented:
Hmm, just read..

"note that the system function works in nawk only." I may have to try that
0
asbharadwajCommented:
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.
0
asbharadwajCommented:
It is getting echoed - didnt notice it earlier ;-)
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

ahoffmannCommented:
print $1, $2, $3, " System command in pattern Section"
0
Chris-MAuthor Commented:
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?
0
ahoffmannCommented:
AFAIK there is no way to catch STDERR and/or STDIN from system() within awk or nawk
use perl instead
0
Chris-MAuthor Commented:
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!
0
ahoffmannCommented:
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
0
ahoffmannCommented:
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.
0
Chris-MAuthor Commented:
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.
0
ahoffmannCommented:
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
0
Computer101Commented:
PAQed, with points refunded (125)

Computer101
E-E Admin
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Linux OS Dev

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.