[SOLVED] Field $() is incorrect 'what does it mean' | Unix Linux Forums | UNIX for Dummies Questions & Answers

  Go Back    


UNIX for Dummies Questions & Answers If you're not sure where to post a UNIX or Linux question, post it here. All UNIX and Linux newbies welcome !!

[SOLVED] Field $() is incorrect 'what does it mean'

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 09-20-2012
Bruble Bruble is offline
Registered User
 
Join Date: May 2010
Last Activity: 26 February 2014, 8:42 AM EST
Posts: 20
Thanks: 2
Thanked 0 Times in 0 Posts
[SOLVED] Field $() is incorrect 'what does it mean'

I am creating (or trying to) an awk script to retrieve various data from an input file and output the data in a different format. I think it is self explanatory what I am attempting but I getting the error Field $() isincorrect when I reun it using

Code:
awk -f file.awk inputfile


Code:
 
#!/bin/awk -f
#get text between 'starting' and 'Ending'
#
BEGIN {
OFS=","
}
#'start loop here'
  /Starting/,/Ending/ {
#if the line contains 'starting'
    if ($0 ~ /starting/) {
       day=substr($0,8,3);
       month=substr($0,12,3);
       date=substr($0,16,2);
       sthour=substr($0,19,2);
       stmin=substr($0,22,2);
       stsec=substr($0,25/2);
       year=substr($0,28,4);
       number=substr($0,33,5);
    }
#if the line contains 'LN'
    if ($0 ~ /LN/) {
       depot=substr($0,58,3);
       order=substr($0,61,5);
    }
#if the line contains 'Ending'
    if ($0 ~ /Ending/) {
       endhour=substr($0,19,2);
       endmin=substr($0,22,2);
       endsec=substr($0,25/2);
    }
 }
#'leave loop here'
durhour=$endhour-$sthour;
durmin=$endmin-$stmin;
dursec=$endsec-$stsec;
END {
  print $year $month $date $sthour $stmin $stsec $endhour $endmin $endsec $durhour $durmin $dursec 
$depot $order
  }

I hope someone can explain why this may be the case.

Thanks in advance.
Sponsored Links
    #2  
Old 09-20-2012
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 17 April 2014, 6:29 PM EDT
Location: Saskatchewan
Posts: 18,531
Thanks: 681
Thanked 3,036 Times in 2,856 Posts
$ does not mean 'variable' in awk. $ is an operator which means 'column'.

i.e, this: print $5 is equivalent to N=5 ; print $N

If you want the value of a variable, and not the column it represents, use it without the $.
Sponsored Links
    #3  
Old 09-20-2012
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 17 April 2014, 5:28 PM EDT
Location: Houston, Texas, USA
Posts: 660
Thanks: 0
Thanked 200 Times in 192 Posts
Remove $ from variable names. Except $0, $1, etc.
    #4  
Old 09-21-2012
Bruble Bruble is offline
Registered User
 
Join Date: May 2010
Last Activity: 26 February 2014, 8:42 AM EST
Posts: 20
Thanks: 2
Thanked 0 Times in 0 Posts
Thanks to you both. This resolved the issue and I understand what the error means now.
Thanks.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
[Solved] sort on numeric part of field bakunin Shell Programming and Scripting 2 08-29-2012 09:00 AM
[Solved] Solaris 10 - Ftp Login incorrect msarro Solaris 10 12-10-2010 02:35 PM
[solved] merging two files and writing to another file- solved mlpathir Shell Programming and Scripting 1 10-07-2010 06:41 PM
login incorrect espace1000 UNIX for Dummies Questions & Answers 2 08-22-2008 06:48 AM
Login Incorrect sydney2008 Red Hat 6 08-22-2008 04:57 AM



All times are GMT -4. The time now is 08:09 AM.