We help IT Professionals succeed at work.

AWK: Pythagoras script

Anthony Mellor
on
239 Views
Last Modified: 2017-03-06
continued from here

Runtime error:
MacPro:Datafiles ADM$ awk -f Pythagoras.awk file1 SITES.CSV >out.txt
awk: syntax error at source line 17 source file Pythagoras.awk
 context is
	   file2 >>>  =? <<< ??SITES.CSV”
awk: illegal statement at source line 18 source file Pythagoras.awk
MacPro:Datafiles ADM$ 

Open in new window


What am I doing wrong?


Code is modified only the filed and file names from the previous answer

# ACC.csv
#CODE,ACC_EC,ACC_NC,Long,Lat,POL,Severity,No_Vehicles,No_Casualties,Date,DayofWeek,Time,LA_District,LA_Highway,1st_Road_Class,1st_Road_Number,Road_Type,LIMIT,Junction_Detail,Junction_Control,2nd_Road_Class,2nd_Road_Number,Zebra-Human,Zebra_Machine,Visibility,Weather_Conditions,Surface,Special_Conditions,Road_Hazards,Urban_Rural,Police_Attended,LSOAofLocation,ACCREF,Month,Minute,ACCDAY,Hour,YEAR,WHEN,ACC_EC5,ACC_NC5
#   a = ACC_EC ($2)
#   b = ACC_NC ($3)
#
# SITES.csv
#  OLD  SORT,POL,REF,CAM ETG,CAM NTG,INSTALL,MONTH,WHEN,TYPE,CAM_ETGkm,CAM_NTGkm
# SORT,POL,REF,SITE_ ETG,SITE_NTG,INSTALL,MONTH,WHEN,TYPE,BEFORE,AFTER
#   r = REF ($3)
#   c = SITE_ ETG ($4)
#   d = SITE_NTG ($5)

# One time logic before records are read from input file
BEGIN {
   # Change field separator from space to comma for our file
   FS = ","
   file2 =“SITES.CSV”
}

# Main processing loop handling each line of the file
{
   # Check if first line (header)
   if (NR == 1) {

      # If this is header just write it to output adding new column headers
      print $0 ",REF,CAM ETG,CAM NTG,Distance"

   } else {

      # skip blank lines
      if ($0 != "") {

         # Save input record from file1
         source = $0

         # Save needed fields for calcs below, remove double quotes if found DOUBLE QUOTES PREVIOUSLY STRIPPED FROM FILE
         a = $2         # ACC_EC 
         b = $3         # ACC_NC
         gsub("\"", "", a)
         gsub("\"", "", b)

         Bestx = 999999999
         # Add new columns at the end
         while ((getline < file2 ) > 0 ) {
            if ($1 != "SORT") {
               r = $3
               c = $4      # SITE_ETG
               d = $5      # SITE_NTG
               x = sqrt(((a-c)^2)+((b-d)^2))
               if (x < Bestx) {
                  Bestx = x
                  Bestr = r
                  Bestc = c
                  Bestd = d
               }
            }
         }
         printf("%s,%s,%s,%s,%f\n", source, Bestr, Bestc, Bestd, Bestx)
         close(file2)
      }
   }
}

Open in new window

Comment
Watch Question

CERTIFIED EXPERT
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Anthony MellorPrincipal
CERTIFIED EXPERT

Author

Commented:
aha, change the quotes with pico...
Anthony MellorPrincipal
CERTIFIED EXPERT

Author

Commented:
MacPro:Datafiles ADM$ awk -f Pythagoras.awk file1 SITES.CSV >out.txt

it's running.. 21:52..
Anthony MellorPrincipal
CERTIFIED EXPERT

Author

Commented:
user a proper editor, noted. thanks

and well spotted!
CERTIFIED EXPERT

Commented:
obviously pico, nano, ee, or even vi or emacs would do

on a mac, textedit should not interfere as long as the file you open is initially text-only if you want some graphical app.

personally, i'm quite fond of jedit which is not nice in terms of look and feel but features good working tools that do not get in the way, remote ssh, tabs, syntax highlighting for most languages, and hypersearch
Anthony MellorPrincipal
CERTIFIED EXPERT

Author

Commented:
pico and nano are about my level.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.