×
UNIX.COM Login
Username:
Password:  
Show Password






👤


UNIX for Beginners Questions & Answers

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

Awk: use variable defined in begin

Tags
awk, begin

👤 Login to reply

 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-14-2017
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 22 February 2018, 1:11 PM EST
Posts: 57
Thanks: 1
Thanked 1 Time in 1 Post
Awk: use variable defined in begin

Hi there,

I'm working with file more than 400K lines, 60 columns. Column count is going to be multiple of 12: 60, 12, 72 or so.
NF/12 gives me on how many iterations I've to do to check certain value.

For example: 7, 14th if only 24 columns in file.
7th, 14th and 21st if 36 columns in file.

I'd like to store and use NF/12 after calculating in BEGIN. But I can't use it.



Code:
head CHR_10_paste.info | awk ' BEGIN{ incr=NF/12 } NR>1 { print $incr }'

incr is 0 so this one-liner prints all the columns.
How do I use it later on once calculated in BEGIN?
Sponsored Links
    #2  
Old 12-14-2017
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 15 July 2018, 11:26 PM EDT
Location: Amsterdam
Posts: 11,853
Thanks: 546
Thanked 3,469 Times in 3,057 Posts
Try:


Code:
print incr

awk does not use a $ sign to get the value stored in the variable..
Sponsored Links
    #3  
Old 12-14-2017
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 22 February 2018, 1:11 PM EST
Posts: 57
Thanks: 1
Thanked 1 Time in 1 Post
It prints 0
    #4  
Old 12-14-2017
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 15 July 2018, 11:26 PM EDT
Location: Amsterdam
Posts: 11,853
Thanks: 546
Thanked 3,469 Times in 3,057 Posts
Oww that is right,
Try:


Code:
FNR==1{ incr=NF/12 }

If NF is the same for every line in the file

In the BEGIN section NF is not yet initialized..

And remove the NR>1 condition

So:


Code:
awk ' FNR==1{ incr=NF/12 } { print incr }'

Sponsored Links
    #5  
Old 12-14-2017
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 16 July 2018, 7:55 AM EDT
Location: Aachen, Germany
Posts: 13,059
Thanks: 447
Thanked 4,008 Times in 3,685 Posts
In the BEGIN section, no file has been opened and read yet, so NF can't be set. And, if I understand correctly, NF changes with every line read, doesn't it? So incr should not be calculated in the BEGIN section, but for every single line.
Sponsored Links
    #6  
Old 12-14-2017
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 22 February 2018, 1:11 PM EST
Posts: 57
Thanks: 1
Thanked 1 Time in 1 Post
NF is going to be constant in a file for all the rows.
A file may have a different number of columns. But they are going to be consistent across all the lines.
I hope I'm making sense.
Sponsored Links
    #7  
Old 12-14-2017
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 16 July 2018, 7:08 AM EDT
Location: Simplicity
Posts: 4,153
Thanks: 365
Thanked 1,418 Times in 1,274 Posts
That's why Scrutinizer suggests to set the incr in line 1.


Code:
NR==1{ incr=NF/12 } { print incr, "line", NR }

A next skips the further processing of the current line.


Code:
NR==1{ incr=NF/12; next } { print incr, "line", NR }


Last edited by MadeInGermany; 12-14-2017 at 12:53 PM..
Sponsored Links
👤 Login to 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
How do I get variable defined in BASH subshell outside? goreilly Shell Programming and Scripting 3 10-09-2014 10:52 AM
Question about awk - create a user-defined variable franksunnn Shell Programming and Scripting 3 06-18-2013 04:11 AM
Finding Variable Value defined in a Script somu_june Shell Programming and Scripting 1 11-17-2009 11:54 AM
Replace variable with a user defined variable ce124 Shell Programming and Scripting 1 04-15-2007 02:56 PM
environment variable is not defined alien12 Linux 0 02-09-2006 10:12 PM



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

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.