Unix/Linux Go Back    


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

UNIX for Beginners Questions & Answers


Tags
awk, begin

Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-14-2017   -   Original Discussion by genome
genome's Unix or Linux Image
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 17 January 2018, 11:35 AM EST
Posts: 51
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 Unix and Linux 12-14-2017   -   Original Discussion by genome
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 19 January 2018, 11:51 AM EST
Location: Amsterdam
Posts: 11,707
Thanks: 530
Thanked 3,399 Times in 2,995 Posts
Try:


Code:
print incr

awk does not use a $ sign to get the value stored in the variable..
Sponsored Links
    #3  
Old Unix and Linux 12-14-2017   -   Original Discussion by genome
genome's Unix or Linux Image
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 17 January 2018, 11:35 AM EST
Posts: 51
Thanks: 1
Thanked 1 Time in 1 Post
It prints 0
    #4  
Old Unix and Linux 12-14-2017   -   Original Discussion by genome
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 19 January 2018, 11:51 AM EST
Location: Amsterdam
Posts: 11,707
Thanks: 530
Thanked 3,399 Times in 2,995 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 Unix and Linux 12-14-2017   -   Original Discussion by genome
RudiC's Unix or Linux Image
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 19 January 2018, 11:36 AM EST
Location: Aachen, Germany
Posts: 11,958
Thanks: 353
Thanked 3,687 Times in 3,385 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 Unix and Linux 12-14-2017   -   Original Discussion by genome
genome's Unix or Linux Image
genome genome is offline
Registered User
 
Join Date: Nov 2017
Last Activity: 17 January 2018, 11:35 AM EST
Posts: 51
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 Unix and Linux 12-14-2017   -   Original Discussion by genome
MadeInGermany's Unix or Linux Image
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 19 January 2018, 1:00 PM EST
Location: Simplicity
Posts: 3,941
Thanks: 334
Thanked 1,317 Times in 1,187 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 01:53 PM..
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 11:52 AM
Question about awk - create a user-defined variable franksunnn Shell Programming and Scripting 3 06-18-2013 05:11 AM
Finding Variable Value defined in a Script somu_june Shell Programming and Scripting 1 11-17-2009 12:54 PM
Replace variable with a user defined variable ce124 Shell Programming and Scripting 1 04-15-2007 03:56 PM
environment variable is not defined alien12 Linux 0 02-09-2006 11:12 PM



All times are GMT -4. The time now is 05:16 PM.