Awk and checking options


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk and checking options
# 1  
Old 10-24-2010
Awk and checking options

My task is that when the user calls the script

1. If user calls script with
awk -v dtmax= -v stdlim= -f ../Scripts/add-rgauss-xt.awk fin.xt > fout.xt
rgauss will return mean + (stdlim * sigma)

2. If user calls script with
awk -v dtmax= -f ../Scripts/add-rgauss-xt.awk fin.xt > fout.xt
rgauss will return mean + (rg * sigma)

I have had a try but getting confused on a good way to achieve this

Code:
  function rgauss(mean, sigma, stdlim,     u1, u2, u3, rg) {
      u1 = (2.0 * rand()) - 1.0
      u2 = (2.0 * rand()) - 1.0
      u3 = (2.0 * rand()) - 1.0
      rg = u1 + u2 + u3
      if ( !stdlim ) {
          return mean + (rg * sigma)
      } else {
          return mean + (stdlim * sigma)
      }
  }

  NF == 2 {
      mean = $2
      sigma = dtmax / 3.0

      if ( !stdlim ) {
          time = rgauss(mean, sigma)
      } else {
          time = rgauss(mean, sigma, stdlim)
      }

  }

  { print }

# 2  
Old 10-24-2010
Perhaps you meant this:
Code:
{ print time }

probably you want it inside the NF==2 condition
# 3  
Old 10-24-2010
Was just wondering if I can have stdlim in rgauss and then check if the user sets it using -v stdlim=

Am getting also confused on local and global variable

Here the variables to rgauss are considered local but then I am trying to pass a global and checking it.
Code:
rgauss(mean, sigma, stdlim,     u1, u2, u3, rg)


Actually there's more to it, like this

dtvals is inputted using -v dtvals=

Code:
  BEGIN {

      srand()           # Get random numbers that are unpredictable
      Version = "V04"
      optdtvals = 0
      optlimits = 0
      optstdlim = 0

      if (ARGV[ARGC-1] == "-usage") {
          usage()
          exit 1
      }

      if (ARGV[ARGC-1] == "-help") {
          help(Version)
          exit 1
      }

      if (ARGV[ARGC-2] == "-dtvals") {
          optdtvals = 1
          ARGV[ARGC-2] = ARGV[ARGC-1]
          delete ARGV[ARGC-1]
      } else if (ARGV[ARGC-1] == "-dtvals") {
          optdtvals = 1
          delete ARGV[ARGC-1]
      }

      if (stdlim) optstdlim = 1

  }

  NF == 2 {
      mean = $2
      sigma = dtmax / 3.0

      if ( !stdlim ) {
          time = rgauss(mean, sigma)
      } else {
          time = rgauss(mean, sigma, stdlim)
      }

      if (optdtvals == 1) {
          time = time - mean
          if ($2 != 0) $2 = time
      } else if (optdtvals == 0) {
          if (($2 != 0) && (time > 0.0)) $2 = time
      }
  }

  { print }



---------- Post updated at 02:54 PM ---------- Previous update was at 02:08 PM ----------




Hi guys, let's consider one thing at a time. I have this code now which should make things easier

Code:
 NF == 2 {
      mean = $2
      sigma = dtmax / 3.0

      if ( !stdlim ) {
          time = rgauss(mean, sigma)
      } else {
          time = rgausslim(mean, sigma, stdlim)
      }
 }

The idea is that is the user does not specify stdlim using -v stdlim= I call rgauss, otherwise I call rgausslim. The problem is that the user can specify -v stdlim=0 and the code will call rgauss instead of rgausslim.

Last edited by kristinu; 10-24-2010 at 04:13 PM..
# 4  
Old 10-24-2010
Try:
Code:
if ( stdlim=="" ) {

# 5  
Old 10-24-2010
Brilliant Smilie
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk - checking last three characters

Hello, I am working with some very large files (upwards of 1M records). I have written code to parse out a lot of the data and am using awk rather than a built-in "while read LINE" for performance (I have tested both ways). That said, I now need to read each of these incoming lines, check the ninth... (2 Replies)
Discussion started by: dagamier
2 Replies

2. Shell Programming and Scripting

Matching options from ARGV in awk

I have written this code in an awk script. BEGIN { print "ARGV", ARGV if ( match(ARGV,/-u/) || match(ARGV,/--usg/) ) { print "MATCH -u:",match(ARGV,/-u/), RSTART, RLENGTH print "MATCH --usg:",match(ARGV,/--usg/), RSTART, RLENGTH usage() exit(1) } } I want... (7 Replies)
Discussion started by: kristinu
7 Replies

3. Shell Programming and Scripting

awk script file command line options

Being new to awk I have a really basic question. It just has to be in the archives but it didn't bite me when I went looking for it. I've written an awk script, placed it in a file, added the "#!/usr/bin/awk -f" at the top of the script and away I go. "% myAwk <inputfile>" gives me exactly what... (2 Replies)
Discussion started by: tomr2k
2 Replies

4. Shell Programming and Scripting

Checking conditions with AWK

Input File1 0BB2 2A11 Split,FriApr80625,1507_7RAID5 0BF6 2829 Synchronized,FriJan140653,1507_7RAID5 0BF6 282A Split,FriApr80625,1507_7RAID5 0C7C 199E Synchronized,FriJan140653,1507_7RAID5 0C7C 1BCC Split,FriApr80625,1507_7RAID5 0DCA 0A9B ... (12 Replies)
Discussion started by: greycells
12 Replies

5. Shell Programming and Scripting

cut, sed, awk too slow to retrieve line - other options?

Hi, I have a script that, basically, has two input files of this type: file1 key1=value1_1_1 key2=value1_2_1 key4=value1_4_1 ... file2 key2=value2_2_1 key2=value2_2_2 key3=value2_3_1 key4=value2_4_1 ... My files are 10k lines big each (approx). The keys are strings that don't... (7 Replies)
Discussion started by: fzd
7 Replies

6. Shell Programming and Scripting

awk if percent % checking

hi everyone, # cat a a 10% b 25.5% c 91% d 50% # cat a | awk '$2 >= 90%; END {print $_}' awk: $2 > 90%; END {print $_} awk: ^ syntax error awk: each rule must have a pattern or an action part how to do only print when 2nd coln >= 90%. Thanks (6 Replies)
Discussion started by: jimmy_y
6 Replies

7. Shell Programming and Scripting

Checking the length using awk??

I have following data in a file a.txt HELLO123456789 HELLO098765432 HELLO322366565 HELLO2343435 HELLO45343 I have to filter those lines whose length is not equal to 14 using awk. Thanks in advance:b: (1 Reply)
Discussion started by: nohup
1 Replies

8. Shell Programming and Scripting

Awk type checking

Hello, How to check if two variables( fields in awk) have the same datatype ( INTEGER , REAL, TEXT ) ? (2 Replies)
Discussion started by: scotty_123
2 Replies

9. Shell Programming and Scripting

options with awk

Hi everybody, I have some difficulties to use awk with the right options (as always): i have for example 3 fields: IF-MIB::ifIndex.1 IF-MIB::ifIndex.2 IF-MIB::ifIndex.3 i want to use "while" to access to these records one by one so i wrote this script but it didn t return the right value:... (2 Replies)
Discussion started by: mips
2 Replies
Login or Register to Ask a Question