embedding awk script in DOS batch file 
Author Message
 embedding awk script in DOS batch file

What is the general technique for embedding a GNU awk script into an
MS-DOS batch file?  I want the batch file to invoke awk, calling
itself as the external awk script. I.e.,


   :: Sample recursive DOS batch file with awk
   echo Starting the batch file
   awk -f %0 %1

The trick is, how do I get awk to skip over the DOS elements of the
batch file and only "see" the awk script parts? My goal is not to
create an intermediate file. That is, I know that I could do this:


   :: DOS batchfile
   echo BEGIN{FS="+"}   >temp.awk
   echo { print $2 }   >>temp.awk
   awk -f temp.awk %1
   del temp >NUL

and in this way I could "embed" an awk script within a DOS batch file.
But I don't want to create an intermediate temp file on disk. I want
to embed the full awk script as a block within the DOS .BAT file.
Failing this, is there a way to accomplish this within a 4DOS .BTM
file? Thanks in advance.



Wed, 28 Jul 2004 03:25:32 GMT  
 embedding awk script in DOS batch file

Quote:

> The trick is, how do I get awk to skip over the DOS elements of the
> batch file and only "see" the awk script parts? My goal is not to
> create an intermediate file. That is, I know that I could do this:

If you manage put the awk script on a single line in the batch, then
you do not need to point (-f) to an intermediate file. There a
several examples in

 213335 Feb 6 2002 ftp://garbo.uwasa.fi/pc/ts/tsbat72.zip
 tsbat72.zip A collection of useful batch files and tricks, T.Salmi

   All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa

Timo's  FAQ  materials  at   http://www.uwasa.fi/~ts/http/tsfaq.html



Wed, 28 Jul 2004 06:07:35 GMT  
 embedding awk script in DOS batch file

Quote:
>What is the general technique for embedding a GNU awk script into an
>MS-DOS batch file?  I want the batch file to invoke awk, calling
>itself as the external awk script. I.e.,


>   :: Sample recursive DOS batch file with awk
>   echo Starting the batch file
>   awk -f %0 %1

>The trick is, how do I get awk to skip over the DOS elements of the
>batch file and only "see" the awk script parts? My goal is not to
>create an intermediate file. That is, I know that I could do this:


>   :: DOS batchfile
>   echo BEGIN{FS="+"}   >temp.awk
>   echo { print $2 }   >>temp.awk
>   awk -f temp.awk %1
>   del temp >NUL

>and in this way I could "embed" an awk script within a DOS batch file.
>But I don't want to create an intermediate temp file on disk. I want
>to embed the full awk script as a block within the DOS .BAT file.
>Failing this, is there a way to accomplish this within a 4DOS .BTM
>file? Thanks in advance.

I don't think it would be possible without a temporary file, at
least if you are including the DOS created ones in the definition
of a temporary file. It is possible to feed the whole batch file
itself to awk, by first locating it's fully qualified file-name
and then feeding that to the -f switch. The problem with such a
method however is that it is not easy to see how to make the same
batch file double up as both an awk script and a DOS batch file
program without a temporary file.

What I mean I suppose is the following;

::
BEGIN{ FS="+"}
{ print $2}





It seems that AWK is likely to always take objection to the DOS commands
that must be present, although there may lie a trick somewhere...
It might be achieved by adding a FIND filter command to the AWK invocation
line;

but that uses a temporary file.

Because you have to supply the filename to AWK, surely the only thing
u can supply is the filename of the existing batch file so I can't
see any way it could work.

You can still however embed the awk script by putting the commands as an
arguement to the -e switch or as the first parameter, directly in the
command line;

AWK -e "BEGIN{ FS=\"+\"};{ print $2}" %1

or
AWK "BEGIN{ FS=\"+\"};{ print $2}" %1

It;s a bit of a nuisance because all quotes need to be escaped by
prefixing them with a backslash meta-character (a subtle gotcha
lies in the fact that DOS will then interpret the special characters
<, >, | that lie inside these escaped quoted strings) but it workz
and in DOS7 with its 1024 character command line length limit you
can fit any typical smallish awk program in there.

byefornow

--

                 # http://lf.8k.com   http://lf.1accesshost.com

"All programs contain at least one bug, and can be shortened by at least
one instruction.
Therefore, all programs can be shortened to a single instruction, which
will be wrong."



Wed, 28 Jul 2004 07:06:30 GMT  
 embedding awk script in DOS batch file

Quote:

> and in this way I could "embed" an awk script within a DOS batch file.
> But I don't want to create an intermediate temp file on disk. I want
> to embed the full awk script as a block within the DOS .BAT file.
> Failing this, is there a way to accomplish this within a 4DOS .BTM
> file? Thanks in advance.

Is it possible for AWK to read the script from a pipe?  I don't know,
but as I understand AWK is interpreted, I suspect that it may be
necessary for the script to come from a disk file (and not stdin.)

If in fact it *is* possible to pipe the script into AWK, one fairly
slick method in 4DOS would be to embed your script in a TEXT / ENDTEXT
block, and pipe from the TEXT statement to AWK.

--



Wed, 28 Jul 2004 08:56:26 GMT  
 embedding awk script in DOS batch file

Quote:

>> and in this way I could "embed" an awk script within a DOS batch file.
>> But I don't want to create an intermediate temp file on disk. I want
>> to embed the full awk script as a block within the DOS .BAT file.
>> Failing this, is there a way to accomplish this within a 4DOS .BTM
>> file? Thanks in advance.

>Is it possible for AWK to read the script from a pipe?  I don't know,
>but as I understand AWK is interpreted, I suspect that it may be
>necessary for the script to come from a disk file (and not stdin.)

This is actually possible, although it seems to only be workable
in GNU-awk using it's special descriptor "/dev/stdin" in the place
of the -f switch (I'm fairly certain that "/dev/std.." is gawk specific
thou`);

echo BEGIN{FS="+"};{print $2} |gawk -f "/dev/stdin" FILENAME

Even though this would facilitate embedding the script in the
batch file and using FIND to weed it out it still has the
problem (described in my post) that >due to the pipe DOS creates
a temporary file anyway<;

:: all awk program lines are prefixed with #!!AWK_CODE identifier
::
BEGIN{ FS="+"}    #!!AWK_CODE
{print $2}        #!!AWK_CODE

:START
::
:: ugly bit of code to locate fully qualified filename of batch
:: and put it into variable BATCHFILENAME would be here

::
:: unfortunately the presence of the pipe is still using a
:: temporary file (albeit now less visible)
FIND "#!!AWK_CODE" %BATCHFILENAME% |GAWK -f "/dev/stdin" FILENAME

Quote:

>If in fact it *is* possible to pipe the script into AWK, one fairly
>slick method in 4DOS would be to embed your script in a TEXT / ENDTEXT
>block, and pipe from the TEXT statement to AWK.

Sounds like a sort of UNIX-here document, I've always been trying
to get this in plain MSDOS but the location of the batch filename
(everything else asides) is quite a pain-- being maybe 20 lines
of code for a clean implementation. William Allen's START command
method for this purpose is much more desireable, being reasonably
simple, however it unfortunately only works in Windows DOS.

regardsfrom

--

                 # http://lf.8k.com   http://lf.1accesshost.com

"All programs contain at least one bug, and can be shortened by at least
one instruction.
Therefore, all programs can be shortened to a single instruction, which
will be wrong."

Quote:

>--




Wed, 28 Jul 2004 08:21:33 GMT  
 embedding awk script in DOS batch file

Quote:



>>> and in this way I could "embed" an awk script within a DOS batch file.
>>> But I don't want to create an intermediate temp file on disk. I want
>>> to embed the full awk script as a block within the DOS .BAT file.
>>> Failing this, is there a way to accomplish this within a 4DOS .BTM
>>> file? Thanks in advance.

>>Is it possible for AWK to read the script from a pipe?  I don't know,
>>but as I understand AWK is interpreted, I suspect that it may be
>>necessary for the script to come from a disk file (and not stdin.)

>This is actually possible, although it seems to only be workable
>in GNU-awk using it's special descriptor "/dev/stdin" in the place
>of the -f switch (I'm fairly certain that "/dev/std.." is gawk specific
>thou`);

>echo BEGIN{FS="+"};{print $2} |gawk -f "/dev/stdin" FILENAME

>Even though this would facilitate embedding the script in the
>batch file and using FIND to weed it out it still has the
>problem (described in my post) that >due to the pipe DOS creates
>a temporary file anyway<;


>:: all awk program lines are prefixed with #!!AWK_CODE identifier
>::
>BEGIN{ FS="+"}    #!!AWK_CODE
>{print $2}        #!!AWK_CODE

>:START
>::
>:: ugly bit of code to locate fully qualified filename of batch
>:: and put it into variable BATCHFILENAME would be here

>::
>:: unfortunately the presence of the pipe is still using a
>:: temporary file (albeit now less visible)
>FIND "#!!AWK_CODE" %BATCHFILENAME% |GAWK -f "/dev/stdin" FILENAME

This was really only meant as an illustration but there is a
small technical error in the above, because the line finds
itself in addition to the embedded awk script lines;

FIND "#!% %!AWK_CODE" %BATCHFILENAME% |GAWK -f "/dev/stdin" FILENAME

seeya

--

                 # http://lf.8k.com   http://lf.1accesshost.com



Wed, 28 Jul 2004 08:28:55 GMT  
 embedding awk script in DOS batch file

Quote:

> If you manage put the awk script on a single line in the batch, then
> you do not need to point (-f) to an intermediate file.

   Thanks, but I'm looking for solution to multi-line scripts.
Besides, some of the scripts I'm thinking about would involve "lines"
longer than 1000 chars, evan after all the unnecessary whitespace was
removed.

   I'm looking for a general solution to the question, which allows
for full blocks of pure awk script, even a couple hundred lines or
more. Thanks. If I run across the solution, I'll post it here myself.

Kind regards,

Eric Pement



Wed, 28 Jul 2004 11:12:57 GMT  
 embedding awk script in DOS batch file


Quote:
>What is the general technique for embedding a GNU awk script into an
>MS-DOS batch file?  I want the batch file to invoke awk, calling
>itself as the external awk script. I.e.,


>   :: Sample recursive DOS batch file with awk
>   echo Starting the batch file
>   awk -f %0 %1

Many, many years ago I did something like this in fortran. It's really
very handy. In my case it was rather the reverse -- in the header
comments of my Fortran program I embedded the batch file I wanted to
use. This was before I ever heard of MAKE but basically I created MAKE
commands inside the Fortran program.

But here is a suggestion on how to satisfy your request:

Here is eric.bat:

echo starting the batch file
awk -f bat_to_awk.awk %0 >pement.awk
awk -f pement.awk %1
del pement.awk

# Below are the AWK commands
BEGIN {
        stuff
        stuff

Quote:
}

/stuff/ {
        stuff
        stuff
        }

END {
        stuff

Quote:
}

# End of eric.bat file

The eric.bat script calls the general-purpose bat_to_awk.awk program.
That program is responsible for extracting the AWK commands from the
DOS batch commands. It's output is a -- temporary file -- pement.awk.

I know you didn't want a temporary file, but a DOS pipe is a temporary
file really. I think you just didn't want to fiddle with the temporary
file yourself (with the tedious echo commands).

I would recommend that if you can use a consistent format for writing
eric.bat-like batch files, the bat_to_awk.awk program will be pretty
easy. For example, the "# Below are the AWK commands" could be enough
to telll the bat_to_awk.awk script where to begin gathering awk code.

I have no doubt you can write the bat_to_awk.awk program.

There might be a file sharing problem. After all, COMMAND.COM has
eric.bat open. If so, sometimes you can still copy %0 to eric.tmp and
pass eric.tmp to bat_to_awk.awk.

Have fun. Please post your solution.

DKM



Wed, 28 Jul 2004 13:05:35 GMT  
 embedding awk script in DOS batch file

Quote:

>This is actually possible, although it seems to only be workable
>in GNU-awk using it's special descriptor "/dev/stdin" in the place
>of the -f switch (I'm fairly certain that "/dev/std.." is gawk specific
>thou`);

I don't yet know enough about awk to discuss it intelligently,
but I did discover this relevant note:

   "An awk command line consists of the command, the script and
   the input filename.  Input is read from the file specified on
   the command line.  If there is no input file or "-" is
   specified, then standard input is read...."

(Dougherty and Robbins, "sed and awk," 2nd edition.)  So I think
my initial guess was wrong, and awk can receive a script via a
pipe.

Quote:
>>If in fact it *is* possible to pipe the script into AWK, one fairly
>>slick method in 4DOS would be to embed your script in a TEXT / ENDTEXT
>>block, and pipe from the TEXT statement to AWK.

>Sounds like a sort of UNIX-here document, I've always been trying
>to get this in plain MSDOS but the location of the batch filename
>(everything else asides) is quite a pain-- being maybe 20 lines
>of code for a clean implementation. William Allen's START command
>method for this purpose is much more desireable, being reasonably
>simple, however it unfortunately only works in Windows DOS.

One "impure" method I've used is to embed the non-batch code in
the batch file with some unique marker prefix for each line; jump
over the embedded code using GOTO; and have the batch filter itself
through sed to pick out the desired lines and strip off the marker
prefix.  Advantages:  simple; no problem embedding fun characters
like < > | % ^ & etc.; you can embed as many different blocks of
non-batch code as you like.  Disadvantage:  you're left with the
ugliness of having the batch determine its own filename.  COMMAND.COM
sometimes seems just too primitive to bother with.

Quote:
>"All programs contain at least one bug, and can be shortened by at least
>one instruction.
>Therefore, all programs can be shortened to a single instruction, which
>will be wrong."

Actually, I *have* a single-instruction program in my collection,
and it is bug-free.  I guess bugs are only inevitable in non-trivial
programs....  <G>

--



Wed, 28 Jul 2004 13:17:17 GMT  
 embedding awk script in DOS batch file

Quote:



>>This is actually possible, although it seems to only be workable
>>in GNU-awk using it's special descriptor "/dev/stdin" in the place
>>of the -f switch (I'm fairly certain that "/dev/std.." is gawk specific
>>thou`);

>I don't yet know enough about awk to discuss it intelligently,
>but I did discover this relevant note:

>   "An awk command line consists of the command, the script and
>   the input filename.  Input is read from the file specified on
>   the command line.  If there is no input file or "-" is
>   specified, then standard input is read...."

>(Dougherty and Robbins, "sed and awk," 2nd edition.)  So I think
>my initial guess was wrong, and awk can receive a script via a
>pipe.

That's useful to know :-)

- Show quoted text -

Quote:

>>>If in fact it *is* possible to pipe the script into AWK, one fairly
>>>slick method in 4DOS would be to embed your script in a TEXT / ENDTEXT
>>>block, and pipe from the TEXT statement to AWK.

>>Sounds like a sort of UNIX-here document, I've always been trying
>>to get this in plain MSDOS but the location of the batch filename
>>(everything else asides) is quite a pain-- being maybe 20 lines
>>of code for a clean implementation. William Allen's START command
>>method for this purpose is much more desireable, being reasonably
>>simple, however it unfortunately only works in Windows DOS.

>One "impure" method I've used is to embed the non-batch code in
>the batch file with some unique marker prefix for each line; jump
>over the embedded code using GOTO; and have the batch filter itself
>through sed to pick out the desired lines and strip off the marker
>prefix.  Advantages:  simple; no problem embedding fun characters
>like < > | % ^ & etc.; you can embed as many different blocks of
>non-batch code as you like.  Disadvantage:  you're left with the
>ugliness of having the batch determine its own filename.  COMMAND.COM
>sometimes seems just too primitive to bother with.

Sure, finding the filename is a pain; it's also very difficult to
formulate a correct/accurate solution for this problem under UNIX,
however there you have the '<<' here document so it doesn't matter
anymore, it sounds like 4DOS does too. Perl also has them and Perl
runs in DOS but to utilise such powerful tools would be just too much
like cheating... ;)

Quote:

>>"All programs contain at least one bug, and can be shortened by at least
>>one instruction.
>>Therefore, all programs can be shortened to a single instruction, which
>>will be wrong."

>Actually, I *have* a single-instruction program in my collection,
>and it is bug-free.  I guess bugs are only inevitable in non-trivial
>programs....  <G>

Oddly enough it was a single-instruction program code that was supposedly
responsible for this saying, and what's more it was most likely the same
program as the one you're referring to. In any case it was called IEFBR14,
and IBM product running under their MVS operating system that had at
least 7 defects filed against it. The code was a simple RETURN instruction
(BR 14 in the assembly language it was written in) but failed to define
a return code (supposed to be in a register it never set) and instead
returned garbage, much like the following code in DOS;

MOV AH,4C               ; AH=4C -- DOS function to exit program
INT 21                  ;          return code required in register AL
                        ;          has not been set by the program

Due to the context this meant the program never returned 0 (SUCCESS)
as it's execution code and so it always failed.

I believe this reference lists the original program and proceeds
to enumerate the various bugs;
http://catless.ncl.ac.uk/Risks/6.14.html#subj1

A brief description;
http://www.notelrac.com/essays.dir/softeng.dir/ibm_nop_bug.html

--

                 # http://lf.8k.com   http://lf.1accesshost.com

"Perfection is achieved, not when there is nothing more to add,
but when there is nothing left to take away" (Antoine de Saint-Exupery)

- Show quoted text -

Quote:
>--




Wed, 28 Jul 2004 12:52:00 GMT  
 embedding awk script in DOS batch file

<snip>
Quote:
> Oddly enough it was a single-instruction program code that was
supposedly
> responsible for this saying, and what's more it was most likely the
same
> program as the one you're referring to. In any case it was called
IEFBR14,
> and IBM product running under their MVS operating system that had at
> least 7 defects filed against it. The code was a simple RETURN
instruction
> (BR 14 in the assembly language it was written in) but failed to
define
> a return code (supposed to be in a register it never set) and instead
> returned garbage, much like the following code in DOS;

Although the IEFBR14 program was vastly increased in size in later
versions of MVS - it now stands as two instructions, one of which sets
register 15 to 0 and the other being the famous BR14.

IEFBR14's an essential part of many JCL jobs.
--
Peter S Tillier
"Who needs perl when you can write dc in sed?"
peter dot tillier at btinternet dot com
To reply direct to me please use the above address
not the "Reply To" which activates a spam trap.



Wed, 28 Jul 2004 16:10:12 GMT  
 embedding awk script in DOS batch file

Quote:


> > If you manage put the awk script on a single line in the batch, then
> > you do not need to point (-f) to an intermediate file.
>    I'm looking for a general solution to the question, which allows
> for full blocks of pure awk script, even a couple hundred lines or
> more. Thanks. If I run across the solution, I'll post it here myself.

Ok, Eric. But let's still stay on the very basics just a little
longer. Is there a special reason or problem why you do not wish the
MS-DOS batch to create the intermediate awk script? The reason why I
ask is that the method of creating the intermediate file(s) when
using language-oriented third party programs (such as awk ports, or
even MS-DOS's own QBASIC) in MS-DOS batches is the common way of
doing it. In a way one could say that it is a part of the MS-DOS
batch game.

   All the best, Timo

--
Prof. Timo Salmi ftp & http://garbo.uwasa.fi/ archives 193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa

Timo's  FAQ  materials  at   http://www.uwasa.fi/~ts/http/tsfaq.html



Wed, 28 Jul 2004 16:23:26 GMT  
 embedding awk script in DOS batch file



Quote:

> > > If you manage put the awk script on a single line in the batch, then
> > > you do not need to point (-f) to an intermediate file.

> >    I'm looking for a general solution to the question, which allows
> > for full blocks of pure awk script, even a couple hundred lines or
> > more. Thanks. If I run across the solution, I'll post it here myself.

> Ok, Eric. But let's still stay on the very basics just a little
> longer. Is there a special reason or problem why you do not wish the
> MS-DOS batch to create the intermediate awk script? The reason why I
> ask is that the method of creating the intermediate file(s) when
> using language-oriented third party programs (such as awk ports, or
> even MS-DOS's own QBASIC) in MS-DOS batches is the common way of
> doing it. In a way one could say that it is a part of the MS-DOS
> batch game.

Creating intermediate scripts (for any language) of a couple hundred lines
or more is slow, especially if the batch is intended for usage from a
floppy. If the generated script requires extensive use of redirection
symbols, then such a long batch becomes excessively and unnecessarily
obfuscated to maintain.

I'm curious why the typical method of using a separate batch as a wrapper is
not being considered? Other than 'doing it for the challenge' of course.

If the batch is intended for distribution along with awk (or whatever),
what's one more file? If it's for personal use in a Windows environment, why
not use a unique extension and just associate/register it. I only ponder
this because someone recently mentioned that amb is little more than a bunch
of people who continually 'reinvent the wheel' over and over. (More or less)

--
Todd Vargo (body of message must contain my name to reply by email)



Wed, 28 Jul 2004 23:41:20 GMT  
 
 [ 25 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Creating an awk script to extract other scripts from a file

2. Embedded awk web scripting language

3. "Embedding" an awk script in bash

4. Calling DOS batch files from within Clipper Code

5. DOS batch file

6. DOS VERSION settings control in batch files

7. Returning a variable to a batch file (DOS)

8. Call to Dos Batch File ??

9. Please help new pyhton user kick off a program from a dos-batch file

10. NT batch file wrapper for python scripts

11. Python on W2K server and dos batch files?

12. Graphical Interface for DOS batch file

 

 
Powered by phpBB® Forum Software