Displaying field of NR, not the line # | 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 !!

Displaying field of NR, not the line #

UNIX for Dummies Questions & Answers


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-06-2012
markymarkg123 markymarkg123 is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 8 January 2013, 3:21 PM EST
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
pamu pamu is offline
Registered User
 
Join Date: Mar 2012
Last Activity: 1 July 2014, 5:40 AM EDT
Posts: 1,649
Thanks: 58
Thanked 478 Times in 474 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
Don Cragun's Avatar
Don Cragun Don Cragun is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 29 July 2014, 1:15 AM EDT
Location: San Jose, CA, USA
Posts: 4,175
Thanks: 163
Thanked 1,421 Times in 1,205 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
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 28 July 2014, 6:27 PM EDT
Location: Saskatchewan
Posts: 18,988
Thanks: 750
Thanked 3,146 Times in 2,951 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
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
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 02:39 AM.