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


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [SOLVED] Field $() is incorrect 'what does it mean'
# 1  
Old 09-20-2012
[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.
# 2  
Old 09-20-2012
$ 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 $.
# 3  
Old 09-20-2012
Remove $ from variable names. Except $0, $1, etc.
# 4  
Old 09-21-2012
Thanks to you both. This resolved the issue and I understand what the error means now.
Thanks.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

[Solved] Awk: count occurrence of each character for every field

Hi, let's say an input looks like: A|C|C|D A|C|I|E A|B|I|C A|T|I|B as the title of the thread explains, I am trying to get something like: 1|A=4 2|C=2|B=1|T=1 3|I=3|C=1 4|D=1|E=1|C=1|B=1 i.e. a count of every character in each field (first column of output) independently, sorted... (4 Replies)
Discussion started by: beca123456
4 Replies

2. UNIX for Dummies Questions & Answers

ID incorrect field values in dat file and output to new file

Hi All I have a .dat file, the values are seperated by ". I wish to identify all field values in field 14 that are not '01-APR-2013' band then copy those records to a new file. Can anyone suggest the UNIX command required. Thanks in advance Andy (2 Replies)
Discussion started by: aurum1313
2 Replies

3. Shell Programming and Scripting

[SOLVED] How to set field separator in awk when FS is "||" ?

Dear Experts, How to use field separator in awk , when the field separation is needed as "||" Example: (file1 ) aa || bb || cc || 1234 xx || yy || zz || 1123 Q: If I want to print $3 and $4 only , with using the field separator "||" How to write awk code: (6 Replies)
Discussion started by: rveri
6 Replies

4. Shell Programming and Scripting

[Solved] Counting specific characters within each field

Hello, I have a file like following: ALB_13554 1 1 1 ALB_13554 1 2 1 ALB_18544 2 0 2 ALB_18544 1 0 1 This is a sample of my file, my real file has 441845 number of fields. What I want to do is to calculate the number of 1 and 2 in each column using AWK, so, the output file looks like... (5 Replies)
Discussion started by: Homa
5 Replies

5. Shell Programming and Scripting

[Solved] sort on numeric part of field

I have ran into a heavy case of PEBCAK*) and could need some advice on what i do wrong: OS is Linux (kernel 2.6.35), sort --version reports "8.5" from 2010, shell is ksh. Originally i had a file with with the following structure: hdisk1 yyy hdisk2 yyy hdisk3 yyy hdisk4 yyy hdisk5 yyy... (2 Replies)
Discussion started by: bakunin
2 Replies

6. Shell Programming and Scripting

[Solved] Need help changing a field from MM/DD/YY to DD/MM/YY format

Hi, I need help changing a field from MM/DD/YY to DD/MM/YY format. Suppose a file a.csv. The record is "11/16/09","ABC"," 1","EU","520892414","1","600","31351000","1234567","ANR BANK CO. LTD" "11/16/09","PQR"," 2","EU","520892427","1","600","31351000","5467897","ANR BANK CO.... (4 Replies)
Discussion started by: Gangadhar Reddy
4 Replies

7. Solaris

[Solved] Solaris 10 - Ftp Login incorrect

Hey everyone, I am trying to get a 2GB patch cluster FTP'd to a solaris 10 server. I have tried logging in via ftp, and both with root as well as my personal account, I get "Login Incorrect." I have verified that I can log in using telnet. -bash-3.00$ netstat -a |grep ftp *.ftp ... (10 Replies)
Discussion started by: msarro
10 Replies

8. Shell Programming and Scripting

[solved] merging two files and writing to another file- solved

i have two files as file1: 1 2 3 file2: a b c and the output should be: file3: 1~a 2~b 3~c (1 Reply)
Discussion started by: mlpathir
1 Replies

9. UNIX for Dummies Questions & Answers

login incorrect

Hi to all, I get a problem when i tried to modifie the password of the root,but thi s the message i have on root password,even i put the old password i have the same message "password has expired, you are allowed to entry by daemon"Could any one give me any suggestion to solve the problem?... (2 Replies)
Discussion started by: espace1000
2 Replies

10. Forum Support Area for Unregistered Users & Account Problems

user name is incorrect

Hello. I registered yesterday with a username of "kailor" and a password of removed. I could only logon with a username of "keithailor@comc" and the above password. I want my username to become "kailor" and my password to remain as removed. Please email me with the steps to correct this. ... (1 Reply)
Discussion started by: keithailor@comc
1 Replies
Login or Register to Ask a Question