Solved

awk script error while running it

Posted on 2006-11-14
4
1,036 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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