Link to home
Start Free TrialLog in
Avatar of aman_greval
aman_greval

asked on

awk script error while running it

I have the follwing awk program:
when i run this program, it waits for user input;
when i give an input, it returns an error:
awk: cmd. line:2: (FILENAME= - FNR=1) fatal: division by zero attempted
why?

#!/bin/awk
awk '
# awkro - expand acronyms
# load acronym file into array "acro"
FILENAME == "acronyms" {
                  split($0, entry, "\t")
                  acro[entry[1]] = entry[2]
                  next}

# process any input line containing caps
/[A-Z] [A-Z]+/ {
            # see if any field is an acronym
            for (i=1; i <= NF; i++)
                  if ($i in acro)
                        { # if it matches add description
                        $i = acro[$i] " (" $i ")" }}

{ #print all lines
      print $0}' acronyms $*
Avatar of ozo
ozo
Flag of United States of America image

That looks more like a shell program that calls awk
Either change
#!/bin/awk
to
#!/bin/sh
or remove the awk '

#!/bin/awk
# awkro - expand acronyms
# load acronym file into array "acro"
FILENAME == "acronyms" {
               split($0, entry, "\t")
               acro[entry[1]] = entry[2]
               next}

# process any input line containing caps
/[A-Z] [A-Z]+/ {
          # see if any field is an acronym
          for (i=1; i <= NF; i++)
               if ($i in acro)
                    { # if it matches add description
                    $i = acro[$i] " (" $i ")" }}

{ #print all lines
     print $0}
Avatar of cjjclifford
cjjclifford

If you change to "#!/bin/awk" remember to use "acronyms" as the first argument to the script (since it was embedded in the original script)
Avatar of aman_greval

ASKER

Here is the acronyms file. you can use this file allong with the above code and find the result:
acronyms:
USGCRP      U.S. Global Change Research Program
NASA      National Aeronautic and Space Administration
EOS      Eatch Observing System

Sol1:
 That looks more like a shell program that calls awk
Either change
#!/bin/awk
to
#!/bin/sh
or remove the awk '
Ans: I tried both but none of it worked

Sol2: If you change to "#!/bin/awk" remember to use "acronyms" as the first argument to the script (since it was embedded in the original script)
I am already supplying the argument acronyms to it, yet the error comes
ASKER CERTIFIED SOLUTION
Avatar of cjjclifford
cjjclifford

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial