Help passing command line parameters to awk scripts 
Author Message
 Help passing command line parameters to awk scripts

I am writing an AWK script that will take the contents of a text file and
generate a postscript file to be printed on a specified form. I would like
to pass command line parameters to the script, to specify formating options,
but I am have trouble getting them into the file. The AWK script is of the
form:

nawk '

  commands

' $*

With the "$*" at the end of the script I can use ARGC and ARGV to get the
command line parameters into the file, but then AWK tries to process the
command line options as files, which it cannot open. If I change to "$1"
the script will only process one file, but I cannot get the command line
parameters into the script.

How can I get command line parameters into the AWK file and still have it
only process one file?

Thanks,

Steve Eslinger

--------------------------------------------------------------------------------
    This company is not responsible for my opinions/words/actions/thoughts/
                    m{*filter*}values/deeds/desires etc.
             For that matter, in our society, neither am I!



Fri, 23 Oct 1998 03:00:00 GMT  
 Help passing command line parameters to awk scripts



Quote:
>I am writing an AWK script that will take the contents of a text file and
>generate a postscript file to be printed on a specified form. I would like
>to pass command line parameters to the script, to specify formating options,
>but I am have trouble getting them into the file. The AWK script is of the
>form:

>nawk '

>  commands

>' $*

>With the "$*" at the end of the script I can use ARGC and ARGV to get the
>command line parameters into the file, but then AWK tries to process the
>command line options as files, which it cannot open. If I change to "$1"
>the script will only process one file, but I cannot get the command line
>parameters into the script.

>How can I get command line parameters into the AWK file and still have it
>only process one file?

nawk '
commands
' var1="x"  var2="y" [...] filename
--

On 22 July, 1996, at 6:00 pm GMT, everyone in the world    
just START HUMMING.  Those who don't know will freak.      


Sat, 24 Oct 1998 03:00:00 GMT  
 Help passing command line parameters to awk scripts


: I am writing an AWK script that will take the contents of a text file and
: generate a postscript file to be printed on a specified form. I would like
: to pass command line parameters to the script, to specify formating options,
: but I am have trouble getting them into the file. The AWK script is of the
: form:

: nawk '

:   commands

: ' $*

: With the "$*" at the end of the script I can use ARGC and ARGV to get the
: command line parameters into the file, but then AWK tries to process the
: command line options as files, which it cannot open. If I change to "$1"
: the script will only process one file, but I cannot get the command line
: parameters into the script.

: How can I get command line parameters into the AWK file and still have it
: only process one file?

: Thanks,

: Steve Eslinger

: --------------------------------------------------------------------------------
:     This company is not responsible for my opinions/words/actions/thoughts/
:                     m{*filter*}values/deeds/desires etc.
:              For that matter, in our society, neither am I!

First,  did you know you can run [ngm]awk scripts directly?

Make the first line:

#!/pat/to/awk -f

Second,  after you process the options,  set ARGV[1] to the name of
the input file you want to process and ARGC=2.  Or,  if you want to
process stdin,  set ARGC=1.

--
Dan Mercer

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



Sat, 24 Oct 1998 03:00:00 GMT  
 Help passing command line parameters to awk scripts

Quote:

> I am writing an AWK script that will take the contents of a text file and
> generate a postscript file to be printed on a specified form. I would like
> to pass command line parameters to the script, to specify formating options,
> but I am have trouble getting them into the file. The AWK script is of the
> form:

> nawk '

>   commands

> ' $*

> With the "$*" at the end of the script I can use ARGC and ARGV to get the
> command line parameters into the file, but then AWK tries to process the
> command line options as files, which it cannot open. If I change to "$1"
> the script will only process one file, but I cannot get the command line
> parameters into the script.

> How can I get command line parameters into the AWK file and still have it
> only process one file?

> Thanks,

> Steve Eslinger

> --------------------------------------------------------------------------------
>     This company is not responsible for my opinions/words/actions/thoughts/
>                     m{*filter*}values/deeds/desires etc.
>              For that matter, in our society, neither am I!

You need to alter the ARGV array (and ARGC) so that only file names are left in it
after processing the parms.  The following code takes the first two arguments,
saves them for processing, and updates the ARGV:

nawk '
BEGIN {
#  Save parms:
  parm_1 = ARGV [1]
  parm_2 = ARGV [2]
#  Shift command-line args over:
  j = 1
  for (i = 3; i < ARGC; i ++) {
    ARGV [j] = ARGV [i]
    j ++
  }  # EndFor
  ARGC -= 2
#  Now print out what's left:
  for (i = 0; i < ARGC; i ++) {
    print i, ARGV [i]
  }  # EndFor
  exit

Quote:
} ' arg1 arg2 arg3 arg4 arg5

The resulting output:

0 nawk
1 arg3
2 arg4
3 arg5

nawk should them try to open only the files "arg3", "arg4", and "arg5".
This is a simple case with no parsing of any of the arguments, but it should
illustrate the idea.  This was run using SunOS 4.1.2 nawk.

Gary B.



Mon, 26 Oct 1998 03:00:00 GMT  
 Help passing command line parameters to awk scripts


Quote:

> I am writing an AWK script that will take the contents of a text file and
> generate a postscript file to be printed on a specified form. I would like
> to pass command line parameters to the script, to specify formating options,
> but I am have trouble getting them into the file. The AWK script is of the
> form:

> nawk '

>   commands

> ' $*

> With the "$*" at the end of the script I can use ARGC and ARGV to get the
> command line parameters into the file, but then AWK tries to process the
> command line options as files, which it cannot open. If I change to "$1"
> the script will only process one file, but I cannot get the command line
> parameters into the script.

> How can I get command line parameters into the AWK file and still have it
> only process one file?

> Thanks,

> Steve Eslinger

> --------------------------------------------------------------------------------
>     This company is not responsible for my opinions/words/actions/thoughts/
>                     m{*filter*}values/deeds/desires etc.
>              For that matter, in our society, neither am I!

After processing the parameter, set corresponding ARGV element to null
string, i.e. if ARGV[i] contains the value of a parameter you don't want
to process as a file, write ARGV[i]="".
--
--------------------------------------------------------------------------------
 .......                                       Yuli Barcohen
    .    ...... .      .....    ..   .....
    .    .      .      .    .  .  .  .    .    CPM design support
    .    .....  .      .    . .    . .    .
    .    .      .      .....  ...... .    .    Phone  972-8-927-3326
    .    .      .      .   .  .    . .    .    Fax    972-8-927-3487

--------------------------------------------------------------------------------


Mon, 26 Oct 1998 03:00:00 GMT  
 Help passing command line parameters to awk scripts


Quote:

> I am writing an AWK script that will take the contents of a text file and
> generate a postscript file to be printed on a specified form. I would like
> to pass command line parameters to the script, to specify formating options,
> but I am have trouble getting them into the file. The AWK script is of the
> form:

> nawk '

>   commands

> ' $*

> With the "$*" at the end of the script I can use ARGC and ARGV to get the
> command line parameters into the file, but then AWK tries to process the
> command line options as files, which it cannot open. If I change to "$1"
> the script will only process one file, but I cannot get the command line
> parameters into the script.

> How can I get command line parameters into the AWK file and still have it
> only process one file?

> Thanks,

> Steve Eslinger

Steve,

I'm assuming from your description of the script that it's a shell script
file which contains the nawk command and its "quoted" instructions.  If
that's so, simply add some more commands prior to "nawk" which retrieve
the command line arguments and parse them.  Then (and you should check the
man pages or other sources for the exact form) use the -V (or is it -F . .
. I'm not sure, since I'm at home and don't have access to my man pages or
awk book) option to set variables.  For example, if you're receiving 3
command line args, process them into three different awk variables via awk
-VVAR1=val1 -VVAR2=val2 -VVAR3=val3 'awkcommands' filename.  That way,
inside the awk commands you can refer to VAR1, VAR2 and VAR3 the same way
you would with NF, NR, $1, or any other predefined awk variables.

Hope this helps.

=Steve=

--
Steve W. Jackson
Montgomery, AL



Wed, 28 Oct 1998 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. THANKS: Help passing command line parameters to awk script

2. Passing command line variables to AWK in shell script

3. Passing parameters via command line

4. Passing parameters via command line

5. Command line passed parameters?!

6. how can one pass command-line parameters to debugger pdb

7. pass regexp to awk on command line

8. Command line options in awk scripts?

9. Help passing parameter in -postcommand line

10. Help pass parameters to commands binded to button

11. help: command line parameters?

12. passing parameter from awk back to korn shell

 

 
Powered by phpBB® Forum Software