Solved

awk script error while running it

Posted on 2006-11-14
4
1,018 Views
Last Modified: 2013-12-26
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 $*
0
Comment
Question by:aman_greval
  • 2
4 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 17943714
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}
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 17945478
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)
0
 

Author Comment

by:aman_greval
ID: 17948192
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
0
 
LVL 11

Accepted Solution

by:
cjjclifford earned 250 total points
ID: 17948470
Sorry, I Was just following up on ozo's comment regarding the incorrect shebang line.

Try the first solution (removing the "#!/bin/awk" ) and my suggestion together...

Actually, save the awk logic (below) as script.awk and execute as:

awk -f script.awk acronyms filename

where filename is the name of the file you want to run your script on...

=== contents of script.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
}

=== END script.awk ====
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
MFC Dialog 9 51
Decoding 32 bit binary streams 6 49
Fibonacci challenge 11 133
ASP.NET C# MessageBox.Show Showing a modal dialog box or form when the application ... 2 166
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question