Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot use Awk with arguments

I am trying to argument processing on awk. It works fine if I have no body in it:

PK@rhel8:~/tmp-> cat testARG.awk 
#!/usr/bin/awk -f
BEGIN{
argc = ARGC ;
CmdName = ARGV[0] ;
FirstArg = ARGV[1]      
printf("Argument count = %d; command name = %s; first argument = %s\n",argc,CmdName,FirstArg) ;
}
#{
#   printf("Argument count = %d; command name = %s; first argument = %s\n",argc,CmdName,FirstArg) ;
#   print $0
#}
PK@rhel8:~/tmp-> ./testARG.awk 1 2 3 4
Argument count = 5; command name = awk; first argument = 1
PK@rhel8:~/tmp-> 

However when I uncomment the body it doesn’t like it at all:

PK@rhel8:~/tmp-> cat testARG.awk 
#!/usr/bin/awk -f
BEGIN{
argc = ARGC ;
CmdName = ARGV[0] ;
FirstArg = ARGV[1]      
printf("Argument count = %d; command name = %s; first argument = %s\n",argc,CmdName,FirstArg) ;
}
{
    printf("Argument count = %d; command name = %s; first argument = %s\n",argc,CmdName,FirstArg) ;
    print $0
}
PK@rhel8:~/tmp-> ./testARG.awk 1 2 3 4
Argument count = 5; command name = awk; first argument = 1
awk: ./testARG.awk:6: fatal: cannot open file `1' for reading (No such file or directory)
PK@rhel8:~/tmp-> 

Is there some different way I have to use awk to allow it to see the arguments as arguments and not files?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You may want to get used to a more standard way of passing non-file args to awk. A common method is to define awk variable assignments on the command line. The general format:

awk -v awk_var1="OS val 1" -v awk_var2="OS val 2" '... awk script ...' [ optional list of filenames]

# or

./script.awk -v awk_var1="OS val 1" -v awk_var2="OS val 2" [ optional list of filenames]

If you won’t be processing any files then all processing within the awk script will need to take place within the BEGIN{} block.

Since the current script is looking to count input args and print the ‘first’, I take it to mean the number of input args could be variable. One common approach would be to provide the args in a single delimited string, eg:

$ cat testARG.awk 
#!/usr/bin/awk -f
BEGIN { n=split(inlist,vars,";")           # split awk input variable "inlist" on a ";" delimiter and put results in the vars[] array
        CmdName = ARGV[0]
        printf "Argument count = %d; command name = %s; first argument = %s\n", n, CmdName, vars[1]
      }

$ ./testARG.awk -v inlist="1;2;3;4"        # define awk variable "inlist" as a ";"-delimited list of values
Argument count = 4; command name = awk; first argument = 1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading