Passing arguments to awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Passing arguments to awk
# 1  
Old 06-22-2010
Passing arguments to awk

I have an awk script below which I call using for example

Code:
awk -f ../../A-Scripts/select-model.awk iterations.txt 16x12 10

I want to be able to use it in a different way like this

Code:
awk -f ../../A-Scripts/select-model.awk iterations.txt nxz=16x12 iter=10

or 

awk -f ../../A-Scripts/select-model.awk iterations.txt iter=10 nxz=16x12

I might like to have some arguments optional as well so if iter is not passed,
the value of iter is defaulted to 10 say.



Code:
  BEGIN {

    if ( ARGC != 4 ) {
      Version = "V01"
      eg1 = "select-model.awk iterations.log 4x3 10 > model-10.xz"
      print ""
      print " ********************************************************"
      print " SCRIPT: select-model.awk "Version
      print " ERROR: Incorrect number of arguments"
      print " USAGE: awk -f select-model.awk fin nxz iter > fout"
      print " E.g. awk -f "eg1
      print ""
      print " CREATED BY: CHRISTOPHER DIMECH"
      print " ********************************************************"
      print ""
      exit 1
    }

    fin = ARGV[ARGC-3]
    nxy = ARGV[ARGC-2]
    iter = ARGV[ARGC-1]
    delete ARGV[ARGC-1]
    delete ARGV[ARGC-2]
    split(nxy, a, "x")
    nx = a[1]
    ny = a[2]
  }

# Arrange the model values in table form  and create the .vmod file

  {
    if ( $2 == iter":" )
    {

      print "% XI=(0,-0.5)"
      print "% XF=(80,20)"
      print ""
      print "%< LAYER 1"
      print ""
      print "% INTI=LIN"
      print "%( INTERFACE"
      print "0 0"
      print "80 0"
      print "%)"
      print ""
      print "% PS=OFF"
      print ""
      print "% INTP=LIN"
      print "%( MODELP"

      skip = 5   # Skip the first 5 fields
      for (i = 0; i < ny; ++i) {
        var = ""
        for (j = skip + 1; j <= NF; j += ny ) {
          var = var FS $(i + j)
        }
        print var
      }

      print "%)"
      print ""
      print "%>"

    }
  }

# 2  
Old 06-22-2010
Execute your script like that:
Code:
awk  -viter=10 -vnxz=16x12 -f ../../A-Scripts/select-model.awk iterations.txt

Add this to your BEGIN clause:
Code:
if (length(iter)==0) iter=10

Also change/delete section that is getting arguments from ARGV to variables, as this won't be needed anymore.

Last edited by bartus11; 06-22-2010 at 09:22 AM.. Reason: Better condition in "if"
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Using and passing arguments to shuf within awk

Hello all, I would like to output a random number within a range for every line using awk and shuf. I think I'm almost there, but I don't know how to pass arguments to shuf within my awk script: Input 1 12190 12227 1 12595 12721 1 13403 13639 1 14362 14829 1 14970 15038 awk: awk '{... (2 Replies)
Discussion started by: DerSeb
2 Replies

2. Shell Programming and Scripting

Passing arguments that contain space

hi All, i am trying to pass arguments that contain space , value will be stored in variables to be used further in script , i went thru previous posting , still its not clear to how to implement for my case. passing 3 args test.sh it is 'fun to work in unix' inside shell ... (3 Replies)
Discussion started by: gvkk
3 Replies

3. Shell Programming and Scripting

Passing arguments--Error

Hi, i have a file.txt with data Bangalore Chennai Hyd filename of the script is: new.sh result=`cat file.txt | grep $1` if then echo pass else echo fail fi i am executing the file in the cmd line as "sh new.sh Bangalore" o/p is pass if i give "sh new.sh delhi" o/p is... (6 Replies)
Discussion started by: harsha85
6 Replies

4. Shell Programming and Scripting

Reading a string and passing passing arguments to a while loop

I have an for loop that reads the following file cat param.cfg val1:env1:opt1 val2:env2:opt2 val3:env3:opt3 val4:env4:opt4 . . The for loop extracts the each line of the file so that at any one point, the value of i is val1:env1:opt1 etc... I would like to extract each... (19 Replies)
Discussion started by: goddevil
19 Replies

5. Shell Programming and Scripting

Passing arguments to csh

I have noticed this thing using csh when passing arguments Suppose I call a csh script using ../Scripts/plot-model.csh -vmod="npt02-z30.vmod" -R="0/80/0/30" -c="0/4.5" -aspr="1:10" Somehow the " get removed when doing $argv ending up with -vmod=npt02-z30.vmod... (0 Replies)
Discussion started by: kristinu
0 Replies

6. UNIX for Dummies Questions & Answers

Passing arguments

I need to pass arguments to a shell script.My batch is calling some java program. ################# x=$1 y=$2 java -classpath program ################### if first parameter and second parameter is null then java -classpath program if first parameter is not null and second parameter is... (3 Replies)
Discussion started by: mnjx
3 Replies

7. Shell Programming and Scripting

passing arguments

Hi I have a script to which I pass multiple arguments, for example lets say the script name is "abc". I run the script like ./abc def /file <directory location> In the above "def" is the first argument and "/file" is the second argument. I expect <directory location> that is passed after... (4 Replies)
Discussion started by: zmfcat1
4 Replies

8. Shell Programming and Scripting

Passing Arguments-Help

Hi, I have a script which adds the user credentials to an ldap server. Im passing the variables as below.. /path/my_script $uname $pwd $environ ${deposit} If i enter some special characters like ';' in $pwd, script returns an error which is set to display if the user enters... (5 Replies)
Discussion started by: Tuxidow
5 Replies

9. UNIX for Dummies Questions & Answers

passing strings as arguments

Is it possible to pass a string as an argument from the command line? I know I can pass a word in but can I put a line of text in with spaces and fullstops or do I just put it in brackets or quotes so the compiler can differinate between the first argument and the second. (1 Reply)
Discussion started by: iago
1 Replies

10. UNIX for Dummies Questions & Answers

passing arguments

I'm trying to pass a filename, or all the files in the current directory to the ls command with a script. Unsuccessful so far, here are a few of my attempts: #!/bin/ksh read fname #if (( $# > 0 )); then $fname | ls -l #fi this produces a long listing of all the files in my current... (4 Replies)
Discussion started by: jpprial
4 Replies
Login or Register to Ask a Question