Command line options in awk scripts? 
Author Message
 Command line options in awk scripts?

I'm in the situation where I need to process a file in several subtly
different ways but do the same fairly complex processing on each.  Let
me illustrate; I may have the following three scripts:

script_1.awk:
#!/usr/bin/awk -f
$1=="A" && $2=="B" {sophisticated action}

script_2.awk:
#!/usr/bin/awk -f
$1=="B" && $2=="A" {sophisticated action}

script_3.awk:
#!/usr/bin/awk -f
$1!=$2             {sophisticated action}

These may be run as follows:
% script_1.awk input.dat > output.dat
% script_2.awk input.dat > output.dat
% script_3.awk input.dat > output.dat

The scripts only differ in the pattern so it seems a shame to have
three scripts repeating the action each time.  The question is, can I
have all three versions in one script, presumably by means of command
line options?

BTW I'm using Gnu Awk (gawk) 2.15, patchlevel 4.

Thanks for any help!

--Chris
--
|                Chris Murphy: Aston Space Geodesy,                 |
| Civil Engineering, The University of Aston, Birmingham B4 7ET, UK |
| TEL : +44 121 359 3611 ext 4552            FAX : +44 121 333 3389 |



Sat, 05 Dec 1998 03:00:00 GMT  
 Command line options in awk scripts?


| I'm in the situation where I need to process a file in several subtly
| different ways but do the same fairly complex processing on each.  Let
| me illustrate; I may have the following three scripts:

| The scripts only differ in the pattern so it seems a shame to have
| three scripts repeating the action each time.  The question is, can I
| have all three versions in one script, presumably by means of command
| line options?

See the -v option.

awk -v FOO='a b c' '
BEGIN { print "Defined in BEGIN:",FOO }
# use ~ for pattern match
{ if ($0 ~ FOO) { print "Matches" }'

Enjoy.
--

"The secret to procrastination is to put things off until the
 last possible moment - but no longer"  -me



Sat, 05 Dec 1998 03:00:00 GMT  
 Command line options in awk scripts?


Quote:

>I'm in the situation where I need to process a file in several subtly
>different ways but do the same fairly complex processing on each.  Let
>me illustrate; I may have the following three scripts:

>$1=="A" && $2=="B" {sophisticated action}

>$1=="B" && $2=="A" {sophisticated action}

>$1!=$2             {sophisticated action}

function action() { sophisticated action }

$1=="A" && $2=="B" { action() }

$1=="B" && $2=="A" { action() }

$1!=$2             { action() }

Quote:
>BTW I'm using Gnu Awk (gawk) 2.15, patchlevel 4.

This is ancient. Get gawk 3.0.0 from a GNU mirror site near you.

--
Arnold Robbins -- guest account at Emory Math/CS        | Laundry increases

"some things are worth preserving." -- Brian Kernighan        | number of children.
upon running `gawk --nostalgia'.                        | -- Miriam A. Robbins



Sun, 06 Dec 1998 03:00:00 GMT  
 Command line options in awk scripts?

What about:

#!/usr/bin/awk -f
BEGIN {
        Case=ENVIRON["CASE"];

Quote:
}

function SophistacedAction()
{
   ...

Quote:
}

$1=="A" && $2=="B"  && Case==CaseOne   {SophisticatedAction}
$1=="B" && $2=="A"  && Case==CaseTwo   {SophisticatedAction}
$1!=$2                        && Case==CaseThree {SophisticatedAction}

Provided you export the CASE variable set to the appropriate value before
calling the script.

Hope this helps,
Patrice.
---------------------------------------------------------------------

|  #include <disclaimer.h>  |   V-mail : (+33) 92 29 39 49 + 3902   |
|-------------------------------------------------------------------|



Sun, 06 Dec 1998 03:00:00 GMT  
 Command line options in awk scripts?

: I'm in the situation where I need to process a file in several subtly
: different ways but do the same fairly complex processing on each.  Let
: me illustrate; I may have the following three scripts:

: script_1.awk:
: #!/usr/bin/awk -f
: $1=="A" && $2=="B" {sophisticated action}

: script_2.awk:
: #!/usr/bin/awk -f
: $1=="B" && $2=="A" {sophisticated action}

: script_3.awk:
: #!/usr/bin/awk -f
: $1!=$2             {sophisticated action}

: These may be run as follows:
: % script_1.awk input.dat > output.dat
: % script_2.awk input.dat > output.dat
: % script_3.awk input.dat > output.dat

: The scripts only differ in the pattern so it seems a shame to have
: three scripts repeating the action each time.  The question is, can I
: have all three versions in one script, presumably by means of command
: line options?

: BTW I'm using Gnu Awk (gawk) 2.15, patchlevel 4.

: Thanks for any help!

: --Chris
: --
: |                Chris Murphy: Aston Space Geodesy,                 |
: | Civil Engineering, The University of Aston, Birmingham B4 7ET, UK |
: | TEL : +44 121 359 3611 ext 4552            FAX : +44 121 333 3389 |

symbolically link the three file names and:

BEGIN {
# always compare constant == variable rather than variable == constant,
# since variable = constant is valid but constant = variable produces
# a syntax error.
test1 = 0
test2 = 0
test3 = 0
if ("script_1.awk" == ARGV[0])
   test1 = 1
else if ("script_2.awk" == ARGV[0])
   test2 = 1
else if ("script_3.awk" == ARGV[0])
   test3 = 1
else {
   print "Unrecognized script name ",ARGV[0]
   exit
   }

Quote:
}

{ doit = 0 }
test1 { doit = ($1=="A" && $2=="B")}
test2 { doit = ($1=="B" && $2=="A")}
test3 { doit = ($1!=$2)}
doit  { sophisticated action }

--
Dan Mercer

Opinions expressed herein are my own and may not represent those of my employer.



Sun, 06 Dec 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Passing command line variables to AWK in shell script

2. Help passing command line parameters to awk scripts

3. THANKS: Help passing command line parameters to awk script

4. command line options for old viscob and vclink commands

5. Problem loading parcels via command-line options

6. Command line options for Visual works 2.5/2.0

7. Executable command line options

8. Command Line Options for Clean Compilers?

9. LOGO-L> Re: Using Command Line Options

10. LOGO-L> Re: Using Command Line Options

11. LOGO-L> Using Command Line Options

12. Command line options parser?

 

 
Powered by phpBB® Forum Software