?
Solved

Awk and its System command

Posted on 2003-10-31
14
Medium Priority
?
388 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:Chris-M
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
  • 2
  • +1
14 Comments
 

Author Comment

by:Chris-M
ID: 9656614
Hmm, just read..

"note that the system function works in nawk only." I may have to try that
0
 
LVL 2

Expert Comment

by:asbharadwaj
ID: 9656783
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
 
LVL 2

Expert Comment

by:asbharadwaj
ID: 9656793
It is getting echoed - didnt notice it earlier ;-)
0
What is a Denial of Service (DoS)?

A DoS is a malicious attempt to prevent the normal operation of a computer system. You may frequently see the terms 'DDoS' (Distributed Denial of Service) and 'DoS' used interchangeably, but there are some subtle differences.

 
LVL 51

Expert Comment

by:ahoffmann
ID: 9677882
print $1, $2, $3, " System command in pattern Section"
0
 

Author Comment

by:Chris-M
ID: 9679414
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9679879
AFAIK there is no way to catch STDERR and/or STDIN from system() within awk or nawk
use perl instead
0
 

Author Comment

by:Chris-M
ID: 9686795
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9686986
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9692882
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
 

Author Comment

by:Chris-M
ID: 9692942
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 9696807
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
 
LVL 1

Accepted Solution

by:
Computer101 earned 0 total points
ID: 9707133
PAQed, with points refunded (125)

Computer101
E-E Admin
0

Featured Post

Veeam Disaster Recovery in Microsoft Azure

Veeam PN for Microsoft Azure is a FREE solution designed to simplify and automate the setup of a DR site in Microsoft Azure using lightweight software-defined networking. It reduces the complexity of VPN deployments and is designed for businesses of ALL sizes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question