Awk: use variable defined in begin

Tags
awk, begin

 
Thread Tools Search this Thread
# 1  
Old 12-14-2017
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?
# 2  
Old 12-14-2017
Try:
Code:
print incr

awk does not use a $ sign to get the value stored in the variable..
# 3  
Old 12-14-2017
It prints 0
# 4  
Old 12-14-2017
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 }'

# 5  
Old 12-14-2017
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.
# 6  
Old 12-14-2017
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.
# 7  
Old 12-14-2017
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..

|
Thread Tools Search this Thread
Search this Thread:
Advanced Search

More UNIX and Linux Forum Topics You Might Find Helpful
Usage of a variable in awk BEGIN nms Shell Programming and Scripting 4 02-26-2018 02:28 PM
Awk: BEGIN: prints nothing genome Shell Programming and Scripting 14 12-07-2017 10:42 AM
How do I get variable defined in BASH subshell outside? goreilly Shell Programming and Scripting 3 10-09-2014 11:52 AM
awk search with begin stew UNIX for Dummies Questions & Answers 9 03-25-2014 01:08 AM
[Solved] Variable defined in .bashrc not intializing in script Ariean UNIX for Dummies Questions & Answers 3 02-13-2014 12:55 AM
Question about awk - create a user-defined variable franksunnn Shell Programming and Scripting 3 06-18-2013 05:11 AM
Defined Variable from text file asavaliya Shell Programming and Scripting 4 03-31-2012 01:43 AM
Finding WHERE an environment variable is defined. dissectcode UNIX for Dummies Questions & Answers 6 04-18-2011 08:59 PM
Unable to add user defined variable ahamed Shell Programming and Scripting 2 01-23-2010 07:29 AM
Finding Variable Value defined in a Script somu_june Shell Programming and Scripting 1 11-17-2009 12:54 PM
awk BEGIN problem cola Shell Programming and Scripting 7 08-18-2009 10:20 AM
awk getting stuck after BEGIN akshaykr2 Shell Programming and Scripting 1 06-24-2009 02:04 PM
Force Input in User Defined Variable earnstaf Shell Programming and Scripting 10 06-20-2007 09:35 AM
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