Solved

awk script error while running it

Posted on 2006-11-14
4
1,001 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Expand LInux Boot partition remotly 3 78
Get filename and folder into excel 7 67
substring method in java 1 79
pre4 challenge 19 87
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

763 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

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now