Hi,
I would like to modify values in a textfile.
Therefore I use awk to extract the values, TRY to convert it and write
it back to the file.
For conversion there is a command line tool (convert), which reads a
value from stdin, does some computation with it and writes the result
to stdout.
Does anybody know, how to express the pseudocode below in awk:
...
for( i...
{
val = convert( $i )
printf( val );
}
...
I've tried something like the following, but witout success.
print $i | "./convert"
It seems that 'convert' will be invoked only one times after
processing the awk script with all parameters together.
What I am doing wrong and how can I get convert's output in a
awk-script variable.
Some ideas or samplecode would be great.
thanks,
klaus
|
|
0
|
|
|
|
Reply
|
klap
|
10/5/2004 3:01:42 PM |
|
grxzmk wrote:
> Hi,
>
> I would like to modify values in a textfile.
> Therefore I use awk to extract the values, TRY to convert it and write
> it back to the file.
> For conversion there is a command line tool (convert), which reads a
> value from stdin, does some computation with it and writes the result
> to stdout.
<snip>
> I've tried something like the following, but witout success.
>
> print $i | "./convert"
>
> It seems that 'convert' will be invoked only one times after
> processing the awk script with all parameters together.
Yeah, you need to close the pipe after each invocation if you want them
treated separately, e.g.:
print $i | "./convert"
close("./convert")
The more natural way to do this, for me at least, is to save the
external command name in a variable and use that for the pipe and close,
e.g.:
{cmd="./convert"; print $i | cmd; close(cmd)}
> What I am doing wrong and how can I get convert's output in a
> awk-script variable.
redirect your commands output to a file, then read that file with
"getline". It's not pretty.
It seems like a plain shell solution would work better for what you're
trying to do. Why are you using awk?
Ed.
|
|
0
|
|
|
|
Reply
|
Ed
|
10/5/2004 4:32:34 PM
|
|
convert <infile >outfile
Or using awk:
awk '{ "convert " $0 }' infile >outfile
|
|
0
|
|
|
|
Reply
|
w_a_x_man
|
10/5/2004 7:04:33 PM
|
|
> awk '{ "convert " $0 }' infile >outfile
Should be
awk '{ system("convert " $0) }' infile >outfile
|
|
0
|
|
|
|
Reply
|
w_a_x_man
|
10/6/2004 2:19:10 AM
|
|
Thanks for the useful hints.
It works fine but the invokation of ./convert really slows down the
conversion on my (QNX) system
cmd = "./convert > tmp";
...
print( $i ) | cmd; close( cmd );
getline res < "tmp"; close( "tmp" );
printf( res );
...
Now I do the conversion in the awk script - it's just a linear
interpolation between 2 lookup-table values - and it works fast enough
(other I've assumed before) to convert really many files in acceptable
time.
I fear, that there is no or no simple plain shell solution because I
have to account other rows (column names, tabname) to decide which
cells in a row have to be converted and the tables are embedded into
other stuff.
By the way it is an oportunity to get familiar with awk and other
tools, it seems to save many time using this tools instead writing a
c-program.
klaus
|
|
0
|
|
|
|
Reply
|
klap
|
10/6/2004 3:35:21 PM
|
|
On Wed, 06 Oct 2004 08:35:21 -0700, grxzmk wrote:
> Thanks for the useful hints.
>
> It works fine but the invokation of ./convert really slows down the
> conversion on my (QNX) system
>
> cmd = "./convert > tmp";
> ...
> print( $i ) | cmd; close( cmd );
> getline res < "tmp"; close( "tmp" );
> printf( res );
> ...
>
> Now I do the conversion in the awk script - it's just a linear
> interpolation between 2 lookup-table values - and it works fast enough
> (other I've assumed before) to convert really many files in acceptable
> time.
>
> I fear, that there is no or no simple plain shell solution because I
> have to account other rows (column names, tabname) to decide which
> cells in a row have to be converted and the tables are embedded into
> other stuff.
> By the way it is an oportunity to get familiar with awk and other
> tools, it seems to save many time using this tools instead writing a
> c-program.
If this were an important program/process then I would experiment with
creating (in a BEGIN section) a (named?) pipe to/from an external program
(like your convert?) or script. That external program would be started up
only once. Then for each value, you should be able to send it out the
pipe, and read the conversion back from the pipe. You would have to take
account of blocking (need flush?) both going in and coming back from that
external program, to prevent things from starving to death. I'm too lazy
to try prototyping that, but the approach should work.
Tho, I suppose, if it were important, you might code it up with something
else. Using awk is best when you can tolerate the overheads of
interpretation, or when you run the script so rarely that it is not worth
optimizing. I don't believe in optimizing everything just for sake of it.
--
Juhan Leemet
Logicognosis, Inc.
|
|
0
|
|
|
|
Reply
|
Juhan
|
10/8/2004 7:57:59 PM
|
|
|
5 Replies
222 Views
(page loaded in 0.092 seconds)
Similiar Articles: Running a script/Invoking a function "in the background" - comp ...Hi all, I need to get a shell script (on Sun Servers) run from ... Running a script/Invoking a function "in the background" ... How can I get a Javascript command to run ... Run an external program directly, not via cmd/command - comp.lang ...XCopy, Unzip, etc... should be able to be just run, no need for command.com to get ... External Shell Execute from abstrakt.com - comp.databases ... Run an external program ... command line length limit - comp.unix.solarisMost things are built > by invoking make! > Which generates a command line, or am I missing ... whether AIX is one of them, but some systems have the shell command ... how to execute a .bat from Javascript? - comp.text.pdfHow can I get a Javascript command to run a Shell script? - comp ... How can I get a Javascript command to run a Shell script? - comp ... invoking shell command - comp ... Is there a way to determine the host machine from within the local ...Not > even a command that the root user would invoke from a shell > command line. > > There was a previous discussion of this in this newsgroup, > and one Sun person ... AWK command inside the shell script - comp.lang.awkAWK question - split string into variables - comp.unix.shell ... AWK command ... Another way is to test the command-line parameters in the shell script before invoking ... Why does `source .bashrc` complain "command not found"? - comp ...If you really need to use bash-isms, then you should invoke bash explicitly. ... > > source is a built-in shell command, not an executable. It is only available from ... Environment variables in an awk script - comp.lang.awkAWK command inside the shell script - comp.lang.awk... the awk variable "i" which has the ... Invoking AWK programs - Heiner's SHELLdorado... way to call AWK scripts ... regutil (Regina) SysFileTree(): requires command line parameter in ...On a related matter, you may consider invoking the interpreter using the '-a ... the command shell will make your code system-specific [and in this case, command shell ... a unix script to send email notification when a sas batch job ...... unix script which is able to do the following 2 things: 1, invoke a ... want to know the exit code for diagnostic purposes, and the shell puts that (i.e. the last command's ... Executing Shell Commands in Emacs | Mastering EmacsYou can use the shell region command to invoke shell code (or python code, or..) by passing something like sh, bash, or python as a command. You may have to adjust shell ... Windows PowerShell Invoke-command Examples | -scriptBlock -filePathPowerShell's invoke-Command is ideal for running a quick command on a remote computer. In addition to -computerName, the main parameters are -scriptBlock and -filePath. 7/23/2012 8:02:08 AM
|