awk script file command line options

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk script file command line options
# 1  
Old 06-29-2011
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 I expect. Now I want to add a command line option to my script to alter its behavior. Something like "% myAwk -opt1 <inputfile>" but awk is interpreting my option as just another input file rather than some internal variable (like: argv[1] ...)

Is what I'm trying to do even possible? If so, what would the awk script look like internally?
- Tom
# 2  
Old 06-29-2011
This gets a bit tricky, especially if you want the awk programme to process a file other than stdin.

Simple case, awk programme will process standard input:

#!/bin/awk    --exec
       for( i = 0; i < ARGC; i++ )
           printf( "argv[%d] = (%s)\n", i, ARGV[i] );
    { print; }

This programme reads and prints the command line arguments, and then prints all of the lines from stdin. All you have to do is interpret the stuff in ARGV. When executing with a file on stdin that has two input lines, this is the output:

spot; t16a  -c bar foo <
argv[0] = (awk)
argv[1] = (-c)
argv[2] = (bar)
argv[3] = (foo)
line 1 in test
Last line in test

The magic here is setting ARGC to 1 in the BEGIN block which causes awk to ignore the rest of the command line. To complicate things, if you want to supply the input file name(s) on the command line, then you'll have to process your parameters and reset ARGV and ARGC accordingly. This is a simple example that accepts a -f value and/or -v parameter(s), and then shifts the file name(s) down:

#!/bin/awk    --exec
        for( i = 1; i <= ARGC; i++ )
            if( substr( ARGV[i], 1, 1 ) != "-" )        # assume first non -x is a file name

            if( ARGV[i] == "-v" )     # example option with no trailing data
                verbose = 1;
                continue;                  # loop to avoid error trap

            if( ARGV[i] == "-f" )      # example option with trailing data
                foo = ARGV[i+1];    # need to validate i+1 isn't out of range
                i++;                # bad form, but it works
                continue;           # loop to avoid error catch

            # suss out other desired options like above

            printf( "unrecognised option: %s\n", ARGV[i] ) >"/dev/stderr";

        j = 1;
        c = 1;
        for( i; i < ARGC; i++ )     # copy input file names down in argv
            ARGV[j++] = ARGV[i];
            c++;                    # new setting for ARGC

        ARGC = c;    # number of file names shifted + 1 for argv[0] value

    { print; }

The command line would be something like:
script-name -v -f "some value" intput-file1 input-file2

Personally, I prefer to wrap my awk with a shell script and let it do all of the command line parsing and other error checking. The script then invokes awk with one or more -v var=value options to pass in the desired data.

Hope this gets you going again.

Last edited by agama; 06-29-2011 at 11:33 PM.. Reason: typo correction
This User Gave Thanks to agama For This Post:
# 3  
Old 06-30-2011
Wow! Thanks for the great explanation! It was easy to follow and exactly what I needed. As you point out, it's not as concise as you could do with a shell script but your implementation is still clean and self-contained.
The only problem I had was with "--exec". Though the awk documentation indicates this is exactly what I need for some reason it didn't like that option and kept displaying the usage message. I changed this to "-f" and used the "--" characters to terminate the awk options ... good enough though not ideal.
Thanks again! ;-)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Read input file with in awk script not through command line

Hi All, Do we know how to read input file within awk script and send output toanother log file. All this needs to be in awk script, not in command line. I am running this awk through crontab. Cat my.awk #!/bin/awk -f function test(var){ some code} { } END { print"test code" } (5 Replies)
Discussion started by: random_thoughts
5 Replies

2. Shell Programming and Scripting

Shell script to read multiple options from file, line by line

Hi all I have spent half a day trying to create a shell script which reads a configuration file on a line by line basis. The idea of the file is that each will contain server information, such as IP address and various port numbers. The line could also be blank (The file is user created). Here... (1 Reply)
Discussion started by: haggismn
1 Replies

3. Shell Programming and Scripting

Reading command line options from bash script

I have the following code and I am calling it using ./raytrac.bash -u and getting problems. For some reason opt_usage is still 0. opt_usage=0 iarg=0 narg=$# while (($iarg < $narg)) do (( iarg = $iarg + 1 )) arg=$argv usrInputFlag=`echo $arg | awk '/=/ {print 1}; ! /=/... (22 Replies)
Discussion started by: kristinu
22 Replies

4. UNIX for Dummies Questions & Answers

Running set options from the command line and bash command

I'm reading about debugging aids in bash and have come across the set command. It says in my little book that an addition to typing set you can also use them "on the command line when running a script..." and it lists this in a small table: set -o option Command Line... (5 Replies)
Discussion started by: Straitsfan
5 Replies

5. Shell Programming and Scripting

Issue with spaces in Java command line options

Hi, I am writing a shell script to build Java options dynamically in a variable array and pass them to java.exe. If an option value contains a space, I cannot find a way to get it interpreted correctly. Here is my example: #!/bin/bash JAVA_HOME=/opt/jvm/jre1.5.0_18 JAVA_OPTS=("-Xms256m... (4 Replies)
Discussion started by: Romain
4 Replies

6. Shell Programming and Scripting

Run perl script, with command-line options

Hello everyone, I have a perl script which takes various command line options from user like : -i <input_file> -o <output_file> -d <value> -c <value> Now I have multiple input files in a directory: <input_file_1> <input_file_2> <input_file_3> <input_file_4> ..... .... ...... (6 Replies)
Discussion started by: ad23
6 Replies

7. Shell Programming and Scripting

Using perl to get options from command line

Hi all, I want to get options from command line by perl. usage() options: -h Show this help message and exit -t Name of tester --timeout Set the timeout -l ... (1 Reply)
Discussion started by: Damon_Qu
1 Replies

8. Shell Programming and Scripting

how to? launch command with string of command line options

my description from another thread... here's my code: #!/bin/bash IFS=$'\n' function OutputName() { input=$1 echo $input input=`echo "$input" | sed -e 's/.//'` input=`echo "$input".avi` output_name=$input } if ]; then echo... (5 Replies)
Discussion started by: TinCanFury
5 Replies

9. Shell Programming and Scripting

Associated array from command line options

I am looking to populate an (associated) array with a command line argument. The command line would look something like this: alert -action test -priority '10' -module test_module . . . The associated array would look like this after the data is read in flag=(action=test priority=10... (1 Reply)
Discussion started by: jperret
1 Replies

10. Programming

Executing command line options

Can someone please tell me how to modify/add to this code so that it recognizes UNIX command options (all beginning with "-") and executes the command with options? #include<stdio.h> #include<stdlib.h> int main(int argc, char *argv) { int i; system("stty -echo"); ... (8 Replies)
Discussion started by: Safia
8 Replies
Login or Register to Ask a Question