INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Passing Parameters to an Executable Awk Script

Passing Parameters to an Executable Awk Script

Passing Parameters to an Executable Awk Script

(OP)
I've spent as couple hours searching in vain for an example of how to pass parameters to an executable awk script. Can anyone lead me to such an example? I tried the following syntax:

#!/bin/gawk -v "var=$1" -f
begin {
print "var=" var
}

Using the command:

>myscript.awk <value> <file-name>

but it says 'var is not a valid variable name.

I've tried everything I could think of that seemed logical, but nothing works.




RE: Passing Parameters to an Executable Awk Script

Hi

The shebang line is not a full featured command line.

Use ARGC and ARGV to access the command line parameters :

CODE --> Awk

#!/usr/bin/gawk -f

BEGIN {
  print "I got", ARGC - 1, "parameters :"

  for (i = 1; i < ARGC; i++)
    print " -", i, "=", ARGV[i]
} 

CODE --> command line

master # .myscript/.awk Hello World!
I got 2 parameters :
 - 1 = Hello
 - 2 = World! 
( Note that ARGV[0] is the process' name. )

Feherke.
feherke.ga

RE: Passing Parameters to an Executable Awk Script

(OP)
Thanks, feherke.ga, but this does not work when processing a text file. My example omitted the detail procesccing. Your example works because there is no 'detail' processing of a text file. My script tries to interpret all parameters as files to be processed.

RE: Passing Parameters to an Executable Awk Script

(OP)
The script fragment I wrote in my original post. However, thinking it wasn't relevent, I left out the detail block, which apparently changes how the parameters are interpreted. Here's my example again with detail processing:

#!/bin/gawk -v "var=$1" -f
begin {
print "var=" var
}
{
print
}

RE: Passing Parameters to an Executable Awk Script

Hi

Then you have to come up with a syntax for calling your script, then implement parameter parsing according to your rules.

For example, I say that my script :
  • will handle values, signaled by preceding them with a "-v" option
  • will handle filenames, not signaled in any way
Then I implement it as :

CODE --> myscript.awk

#!/usr/bin/gawk -f

BEGIN {
  print "I got", ARGC - 1, "parameters :"

  valuefollows = 0
  for (i = 1; i < ARGC; i++) {
    print " -", i, "=", ARGV[i]
    if (valuefollows) {
      print "( this was a value, so removed it from further processing )"
      delete ARGV[i]
      valuefollows = 0
    } else if (ARGV[i] == "-v") {
      print "( -v detected, so the next one is a value )"
      delete ARGV
      valuefollows = 1
    } else {
      print "( nothing special, so kept for further processing )"
    }
  }
}

BEGINFILE {
  print "Starting file", FILENAME, ":"
}

{
  print "Line", FNR, "=", $0
} 

CODE --> command line

master # date | ./awk.awk -- -v Hello txt.txt -v World! -
I got 6 parameters :
 - 1 = -v
( -v detected, so the next one is a value )
 - 2 = Hello
( this was a value, so removed it from further processing )
 - 3 = txt.txt
( nothing special, so kept for further processing )
 - 4 = -v
( -v detected, so the next one is a value )
 - 5 = World!
( this was a value, so removed it from further processing )
 - 6 = -
( nothing special, so kept for further processing )
Starting file txt.txt :
Line 1 =     February 2014
Line 2 = Su Mo Tu We Th Fr Sa
Line 3 =                    1
Line 4 =  2  3  4  5  6  7  8
Line 5 =  9 10 11 12 13 14 15
Line 6 = 16 17 18 19 20 21 22
Line 7 = 23 24 25 26 27 28
Line 8 =
Starting file - :
Line 1 = Fri Feb 28 17:12:45 EET 2014 

Long story short : if you not want a parameter to be processed as input file name, remove it before Awk tries to handle it as such.

Feherke.
feherke.ga

RE: Passing Parameters to an Executable Awk Script

(OP)
Thanks again for your time and promptness in helping me . But your solution seems a bit complex, but informative. I didn't realize I could delete ARGV. That is good to know. However, I think a simpler solution for my purposes would be to utilize the fact that a parameter containing the "=" sign is not considered to be a file name by gawk. I discovered this early on. So I can use ARGV to get a string, such as "num=3" in the BEGIN block and then extract the number 3 from that string. But it would be nice, if the developers would allow the following:

#!/usr/bin/gawk -v num=$1 -f

So I could just execute:
myscript 3 file.txt

For some odd reason gawk says variable 'num' is not legal. Doesn't make any sense to me.

Thanks again. You've been very helpful.



RE: Passing Parameters to an Executable Awk Script


If you continue using your erroneous script, you will get no-where.
Re-read ferherke's post about:

Quote (feherke)

The shebang line is not a full featured command line.
hammer

----------------------------------------------------------------------------
The person who says it can't be done should not interrupt the person doing it. -- Chinese proverb

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close