Matching options from ARGV in awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Matching options from ARGV in awk
# 1  
Old 11-18-2011
Matching options from ARGV in awk

I have written this code in an awk script.



Code:
BEGIN {
  print "ARGV[ARGC-1]", ARGV[ARGC-1]
  if ( match(ARGV[ARGC-1],/-u/) || match(ARGV[ARGC-1],/--usg/) ) {
    print "MATCH -u:",match(ARGV[ARGC-1],/-u/), RSTART, RLENGTH
    print "MATCH --usg:",match(ARGV[ARGC-1],/--usg/), RSTART, RLENGTH
    usage()
    exit(1)
  }
}

I want to call the function usage when I use the following

Code:
awk -f ./addNoise-dat.awk -e
awk -f ./addNoise-dat.awk --examples

However, when I write the below I am still getting the function examples being called. It is because I have included it in a pattern. How can I only check for -e or --examples from the argument list to awk?

Code:
awk -f ./addNoise-dat.awk --exapls

# 2  
Old 11-18-2011
Then match the exact string "-e" or "-examples"...instead of matching on some regular expression pattern.
# 3  
Old 11-18-2011
/-e/ matches both "-e" and "--example"

Code:
/^-e$/  and /^--example$/

are probably what you want to use as your regex
# 4  
Old 11-18-2011
I have tried using the code below, but when I run

Code:
awk -f ./addNoise-dat.awk --exapls

I am getting

Code:
MATCH -e: 2 2 2
MATCH --examples: 0 0 -1

Seems it is matching on -e

Code:
  if ( match(ARGV[ARGC-1],"-e") || match(ARGV[ARGC-1],"--examples") ) {
    print "MATCH -e:",match(ARGV[ARGC-1],"-e"), RSTART, RLENGTH
    print "MATCH --examples:",match(ARGV[ARGC-1],"--examples"), RSTART, RLENGTH
    examples()
    exit(1)
  }

---------- Post updated at 10:21 AM ---------- Previous update was at 10:13 AM ----------

Yes, when I use as mentioned, things are fine.

Is there any way I can group /^-e$/ and /^--examples$/ with | inside match?

Code:
if ( match(ARGV[ARGC-1],/^-e$/) || match(ARGV[ARGC-1],/^--examples$/) )

# 5  
Old 11-18-2011
Your "if" is incorrect because as it is now it will always match whether string is "e" or "examples" or "exapls" so either anchor the strings or do it in two steps...
Code:
if ( match(ARGV[ARGC-1],"^--e$") || match(ARGV[ARGC-1],"^--examples$") ) {
    print "MATCH -e:",match(ARGV[ARGC-1],"-e"), RSTART, RLENGTH
    print "MATCH --examples:",match(ARGV[ARGC-1],"--examples"), RSTART, RLENGTH
    examples()
    exit(1)
  }

Btw i dont know if you can use regexp inside the match function...
# 6  
Old 11-18-2011
Quote:
Originally Posted by kristinu
Is there any way I can group /^-e$/ and /^--examples$/ with | inside match?
Code:
match (ARGV[ARGC-1], /(^-e$|^--examples$)/)

# 7  
Old 11-18-2011
This seems to work

Code:
  if ( match(ARGV[ARGC-1],/^-e$|^--examples$/) ) {
    print "MATCH -e:",match(ARGV[ARGC-1],/^-e$/), RSTART, RLENGTH
    print "MATCH --examples:",match(ARGV[ARGC-1],/^--examples$/), RSTART, RLENGTH
    examples()
    exit(1)
  }

Now, if I write something like

Code:
awk -f ./addNoise-dat.awk --expl

I get

Code:
awk: ./addNoise-dat.awk:221: fatal: cannot open file `--expl' for reading (No such file or directory)

Any idea how I can get awk to output an error if I enter -something or --something and something does not match -e or --examples. This should only happen if the first character starts with -
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to combine all matching dates and remove non-matching

Using the awk below I am able to combine all the matching dates in $1, but I can not seem to remove the non-matching from the file. Thank you :). file 20161109104500.0+0000,x,5631 20161109104500.0+0000,y,2 20161109104500.0+0000,z,2 20161109104500.0+0000,a,4117... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. UNIX for Advanced & Expert Users

O argv, argv, wherefore art thou argv?

All of my machines (various open source derivatives on x86 and amd64) store argv above the stack (at a higher memory address). I am curious to learn if any systems store argv below the stack (at a lower memory address). I am particularly interested in proprietary Unices, such as Solaris, HP-UX,... (9 Replies)
Discussion started by: alister
9 Replies

3. Shell Programming and Scripting

argc/ argv in awk

Hi guys, i'm trying to solve this problem. I have to run something like cat file1.txt | awk -f script.awk 10 if i'm in the awk script, how can i take the parameter :10 ??:wall: i try something like : BEGIN{ var=argv } {..} END{..} but obviously is not correct... (5 Replies)
Discussion started by: heaven25
5 Replies

4. UNIX for Advanced & Expert Users

ISC DHCP ipv4 options and matching ipv6

Hi I am looking to set-up ISC DHCP to support ipv6, and to migrate my existing SUN DHCP ipv4 to ISC, finding the ipv4 options for the SUN DHCP options was ok eg TFTPsrvN = tftp-server-name OptBootF = bootfile-name However if I set up a DHCPv6 does anyone know what the equivalent... (0 Replies)
Discussion started by: eeisken
0 Replies

5. 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

6. Shell Programming and Scripting

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... (4 Replies)
Discussion started by: kristinu
4 Replies

7. Shell Programming and Scripting

print argv inside awk

mode=$1 psg telnetd | awk current=`date +%M`'{ printf ("mode is %s",mode) printf ("mode is %s",ARGV) }' at command prompt when i run the script along with the argument i get only-- 'mode is ' argument is not printed.(If the argument is... (3 Replies)
Discussion started by: Anteus
3 Replies

8. UNIX for Dummies Questions & Answers

argv command in awk

hello does anyone knows how can i reach a parameter in awk command line for example p1 f 4 p1 is an awk script. 4 is parameter i want to work with the second parameter i know it has something to do with argv command i just dont know the syntax. please help. (1 Reply)
Discussion started by: emil2006
1 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