Home Man
Search
Today's Posts
Register

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
# 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 12:53 PM..
Login to Reply

« Previous Thread | Next 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
Usage of a variable in awk BEGIN nms Shell Programming and Scripting 4 02-26-2018 01:28 PM
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
Defined Variable from text file asavaliya Shell Programming and Scripting 4 03-31-2012 12:43 AM
Finding WHERE an environment variable is defined. dissectcode UNIX for Dummies Questions & Answers 6 04-18-2011 07:59 PM
Unable to add user defined variable ahamed Shell Programming and Scripting 2 01-23-2010 06:29 AM
Finding Variable Value defined in a Script somu_june Shell Programming and Scripting 1 11-17-2009 11:54 AM
Force Input in User Defined Variable earnstaf Shell Programming and Scripting 10 06-20-2007 08:35 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 01:48 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password