Solved

Awk and its System command

Posted on 2003-10-31
14
377 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
  • 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
 
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

705 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now