How to specify input file within awk script? 
Author Message
 How to specify input file within awk script?

Hello,

I need to use awk for CGI scripts, therefore I cannot specify input
file on command-line parameters. Any ideas how to specify input
within 'BEGIN' clause? Manual for gawk mentions that
assigning value to ARGV should do, but constructs like
ARGV[ARGIND]="my-input" don't seem to work.

TIA,
Peter.

    ___ _  _                                               _  _ ___
   | . ) |/ /      ------    Piotr Kplicz    ------      \ \| ( . |
   |__||_|\_\     http://www.*-*-*.com/ ~pkeplicz    /_/|_||__|



Sat, 13 Oct 2001 03:00:00 GMT  
 How to specify input file within awk script?

Quote:

> I cannot specify input
> file on command-line parameters. Any ideas how to specify input
> within 'BEGIN' clause?

I use this in one of my programs to get the file name from the command
line :

BEGIN {
 RepOK=0
 do {
  Rep_Log = ARGC > 1 ? ARGV[1] : GetString("Enter file name :")
  RepOK=1
 }  while (! RepOK)

Quote:
}

{ ..... }

END { .... }

function GetString(St) {

 ARGV[1] = "-" # standard input

 print St

 getline

 return($0)

Quote:
}

I'm far from being an awk guru but it works !

Hope it helps.

Christophe



Sat, 13 Oct 2001 03:00:00 GMT  
 How to specify input file within awk script?

Quote:
> BEGIN {
>  RepOK=0
>  do {
>   Rep_Log = ARGC > 1 ? ARGV[1] : GetString("Enter file name :")
>   RepOK=1
>  }  while (! RepOK)

> }

wouldn't:

BEGIN { Rep_Log = ARGC > 1 ? ARGV[1] : GetString("Enter file name :" ) }

be a lot shorter und fulfill the same purpose?

--
Best regards,
 _ __                      _    ,   _ _ _
' )  )     /         _/_  ' )  /   ' ) ) )
 /--' ____/___/> __  /     /--/     / / / __,_  __  o _   ______
/  \_(_) /_) (__/ (_<__   /  ( o   / ' (_(_) (_/ (_<_/_)_(_) / <_

Robert H. Morrison                      Tel:   +49 721 9628 167
Software Development, Basis Team        FAX:   +49 721 9628 149



Sat, 13 Oct 2001 03:00:00 GMT  
 How to specify input file within awk script?

: > I cannot specify input
: > file on command-line parameters. Any ideas how to specify input
: > within 'BEGIN' clause?
:
: I use this in one of my programs to get the file name from the command
: line :

Thanks for help - this gave me, indirectly, an idea what I was doing wrong
when assigning values to ARGV. To specify input within BEGIN clause, so
that it's available for further processing by "pattern actions", one
should do:

#!/usr/bin/awk -f
BEGIN {
        ARGC=2;
        ARGV[1]="my-input";

Quote:
}

{ # process my-input here }

    ___ _  _                                               _  _ ___
   | . ) |/ /     ::::::::   Piotr Kplicz   ::::::::     \ \| ( . |
   |__||_|\_\    http://rainbow.mimuw.edu.pl/~pkeplicz    /_/|_||__|



Sat, 13 Oct 2001 03:00:00 GMT  
 How to specify input file within awk script?

Quote:

> > BEGIN {
> >  RepOK=0
> >  do {
> >   Rep_Log = ARGC > 1 ? ARGV[1] : GetString("Enter file name :")
> >   RepOK=1
> >  }  while (! RepOK)
> > }

> wouldn't:

> BEGIN { Rep_Log = ARGC > 1 ? ARGV[1] : GetString("Enter file name :" ) }

> be a lot shorter und fulfill the same purpose?

Flags are bad, especially in awk where non-null strings are Boolean
truths and one can directly test whether something is or is not.
The reason the flag RepOK is no good is simply because, as you've
pointed out, it's not accomplishing anything. I think the following
is essentially what xtof intended:

BEGIN {
    if (ARGC > 1) {
        Rep_Log = ARGV[1]
    }
    else {
        do {
            Rep_Log = GetString("Enter file name: ")
            # Error checking to ensure the validity of the
            # input file name goes here!
        }
        while (!Rep_Log)
    }

Quote:
}

And there's no need to redundantly check the value of ARGC in a
do..while loop.

There are a gazillion ways to prompt for stuff and check the user's
input, none of which are specific to the awk programming language.
For EYEWTKA prompting users for input, see comp.programming.

--
Jim Monty

http://www.primenet.com/~monty/
Tempe, Arizona USA



Sat, 13 Oct 2001 03:00:00 GMT  
 How to specify input file within awk script?
Hello all

Quote:

> function GetString(St) {

>  ARGV[1] = "-" # standard input

>  print St

>  getline

>  return($0)

> }

I am by no means an awk expert and I have never written a CGI script, so I
am certainly not aware of the hole problem with this thread. But still, I
couldn't find a reason not to replace the function GetString above with

function GetString(St,       local_str)  # local_str is a local variable
print St
getline local_str < "-"                        # read a line from standard
input
return(local_str)

This function does not change the main input stream, wich, in my opinion,
is a good thing. If that is the required behaviour, I mean, if you really
want to read the name of a file to be included in the main input stream,
then you have to deal with the ARGC and ARGV variables, for intance
inserting the next two lines before the return line
++ARGC
ARGV[ARGC-1]=local_str

I only use gawk, but I believe that this should work in any POSIX awk. Is
there any reason not to do it like this?

thanks.
Ze amoreira



Mon, 22 Oct 2001 03:00:00 GMT  
 How to specify input file within awk script?

: I am by no means an awk expert and I have never written a CGI script, so I
: am certainly not aware of the hole problem with this thread.

The problem is that when you execute a script "directly" from the
shell (it's got to begin with a '#!/usr/bin/awk -f' line) - and
that's the way CGI scripts are executed - you have very little
control over command line parameters. You cannot, e.g., specify an
input file. Of coz, you can create a shell script that will would
call the awk script but that's an ugly solution.

: ++ARGC
: ARGV[ARGC-1]=local_str
:
: I only use gawk, but I believe that this should work in any POSIX awk. Is
: there any reason not to do it like this?

There ain't any :) In fact I came up to this solution eventually.

    ___ _  _                                               _  _ ___
   | . ) |/ /      ------    Piotr Kplicz    ------      \ \| ( . |
   |__||_|\_\    http://rainbow.mimuw.edu.pl/~pkeplicz    /_/|_||__|



Mon, 22 Oct 2001 03:00:00 GMT  
 How to specify input file within awk script?
Good and useful.  Now is there any way to make the input come from
another process?  What I'm doing now is:

#!/bin/sh
/sbin/ifconfig|/bin/gawk '/^ppp0/{on=1} /inet addr:/{if (on) print substr($2,6)}'

But I'd rather be able to make the gawk process the main one, but
still get the input from a pipe.  Does that make any sense?
--

http://cec.wustl.edu/~adl4/



Sat, 27 Oct 2001 03:00:00 GMT  
 How to specify input file within awk script?


Quote:
> Good and useful.  Now is there any way to make the input come from
> another process?  What I'm doing now is:

> #!/bin/sh
> /sbin/ifconfig|/bin/gawk '/^ppp0/{on=1} /inet addr:/{if (on) print
substr($2,6)}'

> But I'd rather be able to make the gawk process the main one, but
> still get the input from a pipe.  Does that make any sense?

I think you're after

/bin/gawk 'BEGIN{c="/sbin/ifconfig"
  while (c | getline) {
    on = on || /^ppp0/
    if (/inet addr:/ && on) print substr($2, 6)
  }
  close(c) # unnecessary in this case, but force of habit
  exit

Quote:
}'

It can be done, but stuffing everything into the BEGIN block is
inelegant. What's the problem with leaving the pipe at shell level?

--== Sent via Deja.com http://www.deja.com/ ==--
---Share what you know. Learn what you don't.---



Sat, 27 Oct 2001 03:00:00 GMT  
 How to specify input file within awk script?

Quote:

> Good and useful.  Now is there any way to make the input come from
> another process?  What I'm doing now is:

> #!/bin/sh
> /sbin/ifconfig|/bin/gawk '/^ppp0/{on=1} /inet addr:/{if (on) print substr($2,6)}'

> But I'd rather be able to make the gawk process the main one, but
> still get the input from a pipe.  Does that make any sense?

#!/bin/gawk
BEGIN {
ifconfig = "/sbin/ifconfig"
while ((ifconfig | getline) > 0)
   {
   if ($0 ~ /^ppp0/)
      on = 1
   if (($0 ~ /inet addr:/) && on)
      print substr($2,6)
   }

close(ifconfig)
exit

Quote:
}

This exactly reproduces your script.

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.



Sat, 27 Oct 2001 03:00:00 GMT  
 How to specify input file within awk script?
Well, it works either way, I'm actually more interested in just
learning different ways of approaching it.  And it felt inefficient to
run sh just to do the piping (yeah, I know it doesn't matter).  I'm
just wondering whether, if I hit something like this again, there's
another way.  I want something like

#!/bin/gawk -f
BEGIN{
  #turn stdin into the input from /sbin/ifconfig

Quote:
}

/^ppp0/ {on=1}
on&&/inet addr:/ {print substr($2,6)}

It really doesn't matter much, I just want to know whether it can be done.
--

http://cec.wustl.edu/~adl4/



Sun, 28 Oct 2001 03:00:00 GMT  
 How to specify input file within awk script?


% just wondering whether, if I hit something like this again, there's
% another way.  I want something like
%
% #!/bin/gawk -f
% BEGIN{
%   #turn stdin into the input from /sbin/ifconfig
% }

Look at the preceding post again. It answers this question. Keep in mind that,
from an efficiency standpoint, there's no difference between
 #!/bin/sh
 ifconfig ... | awk '{ print $2 }'
and
 #!/bin/awk -f
 BEGIN { while ("ifconfig ..." | getline) print $2 }

since awk will simply run ifconfig as /bin/sh -c ifconfig. I guess you could
save on something if you made the command "exec ifconfig ..."
--

Patrick TJ McPhee
East York  Canada



Mon, 29 Oct 2001 03:00:00 GMT  
 How to specify input file within awk script?

Quote:

>Well, it works either way, I'm actually more interested in just
>learning different ways of approaching it.  And it felt inefficient to
>run sh just to do the piping (yeah, I know it doesn't matter).  I'm
>just wondering whether, if I hit something like this again, there's
>another way.  I want something like

>#!/bin/gawk -f
>BEGIN{
>  #turn stdin into the input from /sbin/ifconfig
>}

>/^ppp0/ {on=1}
>on&&/inet addr:/ {print substr($2,6)}

>It really doesn't matter much, I just want to know whether it can be done.
>--

>http://cec.wustl.edu/~adl4/

Yes, it can be done either way. As you imply, it doesn't matter because even if
you use an awk pipe to getline, awk will fire up a shell in order to run the
left hand side of the pipe. Keeping the pipe outside of awk at least lets you
avoid having to do the process and I/O handling explicitly in awk code.


Mon, 29 Oct 2001 03:00:00 GMT  
 
 [ 13 post ] 

 Relevant Pages 

1. Replacing a string from an input file within awk script

2. piping input files into awk script

3. Access to Script Name Within Awk Script

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

5. trying to call awk from within a shell script

6. Run awk from within sed script?

7. shell commands within awk script

8. using Oracle within an awk script

9. Simple awk command within a perl script

10. awk within ksh script

11. Running Awk scripts from within VB

12. Shell variable within an awk script

 

 
Powered by phpBB® Forum Software