Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Parameters to an Executable Awk Script

Status
Not open for further replies.

oldwhtman

Programmer
Feb 28, 2014
4
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.




 
Hi

The shebang line is not a full featured command line.

Use [tt]ARGC[/tt] and [tt]ARGV[/tt] to access the command line parameters :
Code:
[gray]#!/usr/bin/gawk -f[/gray]

[b]BEGIN[/b] {
  [b]print[/b] [green][i]"I got"[/i][/green], ARGC - 1, [green][i]"parameters :"[/i][/green]

  [b]for[/b] (i = 1; i < ARGC; i++)
    [b]print[/b] [green][i]" -"[/i][/green], i, [green][i]"="[/i][/green], ARGV[i]
}
Code:
[blue]master #[/blue] .myscript/.awk Hello World!
I got 2 parameters :
 - 1 = Hello
 - 2 = World!
( Note that [tt]ARGV[0][/tt] is the process' name. )

Feherke.
feherke.ga
 
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.

 
My script
Which script ?

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
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
}
 
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 :
[ul]
[li]will handle values, signaled by preceding them with a "-v" option[/li]
[li]will handle filenames, not signaled in any way[/li]
[/ul]

Then I implement it as :
Code:
[gray]#!/usr/bin/gawk -f[/gray]

[b]BEGIN[/b] {
  [b]print[/b] [green][i]"I got"[/i][/green], ARGC - 1, [green][i]"parameters :"[/i][/green]

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

[b]BEGINFILE[/b] {
  [b]print[/b] [green][i]"Starting file"[/i][/green], FILENAME, [green][i]":"[/i][/green]
}

{
  [b]print[/b] [green][i]"Line"[/i][/green], FNR, [green][i]"="[/i][/green], $0
}
Code:
[blue]master #[/blue] 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
 
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.



 

If you continue using your erroneous script, you will get no-where.
Re-read ferherke's post about:
feherke said:
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
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top