Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

awk script error while running it

Posted on 2006-11-14
4
Medium Priority
?
1,066 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 85

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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month11 days, 21 hours left to enroll

916 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