Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers


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

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-06-2012
Registered User
 
Join Date: Dec 2012
Posts: 10
Thanks: 5
Thanked 0 Times in 0 Posts
Displaying field of NR, not the line #

Within AWK, how do you display a field of NR? Here's my code:


Code:
awk '(NR>1) && (P1=$1-w)>=100000 {print "increase of" " " P1*.0000179," " "kW at" " " 'NR*60/431900' " " "minutes" "\n" "change from" " " 'NR-10($1)' " " "kW to" " " 'NR+70($1)' "\n"}{w=$1}' filename

I can change NR and print line #'s, but cannot get a field to print...the error comes at the segment
Code:
'NR-10($1)' " " "kW to" " " 'NR+70($1)'

Sponsored Links
    #2  
Old 12-07-2012
Registered User
 
Join Date: Mar 2012
Location: Pune, India
Posts: 1,485
Thanks: 56
Thanked 425 Times in 421 Posts
Quote:
Originally Posted by markymarkg123 View Post

Code:
awk '(NR>1) && (P1=$1-w)>=100000 {print "increase of" " " P1*.0000179," " "kW at" " " 'NR*60/431900' " " "minutes" "\n" "change from" " " 'NR-10($1)' " " "kW to" " " 'NR+70($1)' "\n"}{w=$1}' filename

Why you are using single quotes inside awk..? (and why so many double quotes.. ? )

You can directly use NR.
Sponsored Links
    #3  
Old 12-07-2012
Registered User
 
Join Date: Jul 2012
Location: San Jose, CA
Posts: 1,454
Thanks: 62
Thanked 523 Times in 457 Posts
Quote:
Originally Posted by markymarkg123 View Post
Within AWK, how do you display a field of NR? Here's my code:


Code:
awk '(NR>1) && (P1=$1-w)>=100000 {print "increase of" " " P1*.0000179," " "kW at" " " 'NR*60/431900' " " "minutes" "\n" "change from" " " 'NR-10($1)' " " "kW to" " " 'NR+70($1)' "\n"}{w=$1}' filename

I can change NR and print line #'s, but cannot get a field to print...the error comes at the segment
Code:
'NR-10($1)' " " "kW to" " " 'NR+70($1)'

Your awk program (after unneeded quote removal) is:

Code:
awk '(NR>1) && (P1=$1-w)>=100000 {print "increase of " P1*.0000179 "  kW at " NR*60/431900 " minutes\nchange from " NR-10($1) " kW to " NR+70($1) "\n"}{w=$1}' filename

Which after throwing away the strings from the print statement, leaves 4 expressions to be evaluated and printed: P1*.0000179 , NR*60/431900 , NR-10($1) , and NR+70($1) . The first two of these are valid expressions in awk; the last two are not. What were you expecting those expressions to do?

Without having a sample of your input file, an example of the output you expect to produce, and a description of what you're trying to do; there isn't any way that we can help correct your code.
    #4  
Old 12-07-2012
Mead Rotor
 
Join Date: Aug 2005
Location: Saskatchewan
Posts: 16,371
Thanks: 490
Thanked 2,534 Times in 2,417 Posts
To answer your question directly, $ is the 'column' operator in awk.

$(1+1) gets you column 2.

$VARIABLE gets you the column number given by the variable.


So, $NR.
Sponsored Links
Reply

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
Displaying the first field if the second field matches the pattern using Perl royalibrahim Shell Programming and Scripting 7 11-06-2012 11:52 AM
sed to replace a field from a line with another field vivek d r Shell Programming and Scripting 1 04-13-2012 10:57 AM
Compare Field in Current Line with Field in Previous moutaye Shell Programming and Scripting 9 03-27-2012 05:18 AM
Displaying a field completely polavan Shell Programming and Scripting 3 12-07-2011 08:01 AM
Displaying lines of a file where the second field matches a pattern LordJezoX Shell Programming and Scripting 3 05-20-2009 08:00 AM



All times are GMT -4. The time now is 07:08 PM.