Visit Our UNIX and Linux User Community


Awk: Assigning a variable to be the value of FNR at a certain line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Awk: Assigning a variable to be the value of FNR at a certain line
# 1  
Old 12-05-2015
Awk: Assigning a variable to be the value of FNR at a certain line

Sorry for the probably strangely worded title but I don't really know how else to put it.

Background context: Post processing LAMMPS simulation data.
tl;dr: I'm making two spheres collide, every defined timestep the simulation outputs a bunch of data including total energy of the particles, their coordinates, etc...

Here is my problem:

I'm using awk to post process this data, what I need right now is for it to find the closest point two values approach, give me the closest approach distance (called overlap in the code since it's actually two particles overlapping slightly) and give me the line number that happens on.

So X1 & X2 will first approach each other, reach a closest point then distance themselves from each other.
Finding the closest distance is not a problem.
I do it thus:
The relative code includes the bits with 'overlap' 'max' 'dx' 'z1' 'z2' etc..

The other code is for a similiar problem: Once the energy reaches equilibrium I want that equilibrium and the line where it happens.
However in this case that's not a problem, since it will be the last line treated so FNR is correct.
Code:
BEGIN{
oldte=0
te2=1
max=0
OFS=","			# Changes Output Field Seperator to a comma (default is space) 
				# so it can be appended to a .csv file which excel opens without a prompt
printf("%s,%s,%s,%s,%s,%s,%s,\n","Change","Et1","Et2","ColDt","dt","steps","max")

}
{
	if((FNR+1)%11==0)
	{
		te1=$5
		z1=$1
		
	}
	
	if(FNR%11==0){
		te2=$5
		dt=$6
		z2=$1
		dx=(z2-z1)
		overlap=(0.05-dx)
		if(overlap>max)
		{
			max=overlap							# overlap calculations
		}
		if(oldte==te2)
		{
			steps=FNR/11
			colDt=dt*steps
			printf("%s,%s,%s,%1.8f,%s,%s,%1.8f\n",change,te1,te2,colDt,dt,steps,max)
			exit														
		}
		else
		{
		oldte=$5
		}
		
	}
		
}
END{

}


So the problem here is that I can't use

Code:
ClosestPoint=FNR/11

in the if expression for finding the max value of overlap because when I call that variable to be printed along with the other data, it will be reevaluated and use the FNR at that point in the input file (which will be to far down so to speak).


It's probably a lot more explanation then needed for my question but I thought I'd give as much context as possible.

I guess it just comes down to this:

Can I set a variable X to equal the value of NFR at the time that X is set and not have X be reevaluated when I print it.
# 2  
Old 12-05-2015
I am having trouble understanding your problem..

In general I can say that is you are processing data and can only find out if a certain line contains the right data later one or more lines later on, then you could record the data into a variable. For example if you want to use printf(...) to print a value, instead you can use some_var=sprintf(...) to record an output line into a variable, and print this recorded variable (print some_var) one or more lines down when you are able to conclude that it was indeed the right value..

Hope this helps and/or makes sense...
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 12-05-2015
So f.e.

Code:
some_var=sprintf(NFR)

and then later
Code:
print(some_var)

Would give me the original value and some_var would not be reevaluated as being =NFR ?
# 4  
Old 12-05-2015
Right. Of course in this example, you would record somevar=FNR , I only used sprintf() as an example so you can record including the formatting.

This works if the proper line is the previous line. If it can be several lines back, you can use a "circular buffer" by using an array,

For example:

A[FNR%4]=sprintf(...) would record the last 4 lines, so you can select which one to print...
# 5  
Old 12-05-2015
Ah yes see that's the problem, the 'overlap' NFR will always be several (100+) lines before the oldte==te2 (energy equilibrium) NFR, because of how the simulation works.

So if the some_var still gets evaluated as =NFR when I print it at the end
f.e. print(some_var) then i'll have the wrong value. On the other hand if it only gets evaluated as being = an integer (a previous NFR in this case) than I'm golden.

An array wouldn't help me since I can't know which line I need, that's the whole point of getting the NFR value at that point.
# 6  
Old 12-05-2015
I'm not at all sure that I understand what you're trying to do, but if you change:
Code:
		if(overlap>max)
		{
			max=overlap							# overlap calculations
		}

to:
Code:
		if(overlap>max)
		{
			max=overlap							# overlap calculations
			max_line=FNR
		}

then when you get to the END section of your awk script, the variable max contains your largest overlap you found and the variable max_line contains the line number in your input file where you found that maximum.
# 7  
Old 12-05-2015
That's the thing I did something similar and it didn't give me the correct FNR instead it seemed to reevaluate the variable (max_line in your example) to =NFR

Although I'm not sure whether I put that printf() in the END section, then it the variable might not get reevaluated

Previous Thread | Next Thread
Test Your Knowledge in Computers #346
Difficulty: Easy
AWK was created at Bell Labs in the 1960s,
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Tcsh command for assigning output of awk to variable

Hi I have a text file with 2 values and I am trying to assign each value to a variable and then write those to text files. So if the textfile is data.txt with 2 values x and y I want to assign mean=x, and stdev=y and then write these out in text files alongwith the id ($id has already been... (6 Replies)
Discussion started by: violin
6 Replies

2. Shell Programming and Scripting

Assigning Variable to Line Numer in nl

hi, i'm creating a little menu for some users. i'm running the command: du -a /apps | sort -n -r | head -n 10 | nl i then get the top 10 files by size in the /apps directory the output is like this: 1 101415752 /apps 2 89188064 /apps/userA 3 74521335 ... (1 Reply)
Discussion started by: horhif
1 Replies

3. Shell Programming and Scripting

Assigning output from awk to variable

I have a script whose contents are as below result= awk 's=100 END {print s }' echo "The result is" $result The desired output is The result is 100 My script is running without exiting and i am also not getting the desired output. Please help (5 Replies)
Discussion started by: bk_12345
5 Replies

4. Shell Programming and Scripting

Assigning a value to variable through awk

I am trying to assign a value to a variable thru awk and I am having a lot of problem with it. Pls see the code snippet. The one in RED is the actual code. Other lines are the op created by the system. As you can see from the data, I was expecting an output of DG010 SDS FILE for FILE_NAME... (6 Replies)
Discussion started by: vskr72
6 Replies

5. Shell Programming and Scripting

problem in assigning substr to a variable inside awk

Hi All, I have a fixed-width datafile from which i need to extract value/string starting from some position to the specified length in each of the lines. awk '{print substr($0,x,y)}' datafile --- is working fine but awk 'BEGIN{a=0}{a=substr($0,x,y);print $a}' datafile ---is giving... (3 Replies)
Discussion started by: loggedin.ksh
3 Replies

6. Shell Programming and Scripting

how to Read a file and assigning each line to a variable?

Friends, I have a file output.txt with values as below: 092307135717 061910135717 I want to know how to read this file and then assign each value to a variable. say like var1=092307135717 var2=061910135717 So that I can use this VAR1 and Var2 in the shell script for further processing.... (3 Replies)
Discussion started by: shyamaladevi
3 Replies

7. Shell Programming and Scripting

Assigning last line to variable in expect

As part of an expect script, I have to convert a strange user ID to a conventional UNIX ID. To do this, I read the contents of a file and do a little awk magic. Here's that bit of the expect script: send "awk 'NF == 10 && \$NF == strange_user_id {print \$(NF-2)}' file_with_my_info\r" expect... (0 Replies)
Discussion started by: treesloth
0 Replies

8. UNIX for Dummies Questions & Answers

Assigning the output of a command to a variable, where there may be >1 line returned?

Hello I am using unix CLI commands for the Synergy CM software. The command basically searches for a folder ID and returns the names of the projects the folder sits in. The result is assigned to a variable: FIND_USE=`ccm folder -fu -u -f "%name"-"%version" ${FOLDER_ID}` When the command... (6 Replies)
Discussion started by: Glyn_Mo
6 Replies

9. Shell Programming and Scripting

Assigning Variable to AWK statement

Hi, The following command runs on in the Korn shell prompt. however i want to output the value of this to a variable. Can anyone provide a solution? echo 'ABC,DEF,"G,HI,J",KLM,"MNi,O"'| awk -F "\"" '{for(i=1;i<=NF;i++){if(i%2)gsub("\,","~^~",$i)}}1' (2 Replies)
Discussion started by: ladarlsan
2 Replies

10. Shell Programming and Scripting

Problem with assigning output of grep + awk to a variable

Hi All, I am getting the output for the following command when i run it on the unix console. --------------------------- grep `whoami` /etc/passwd | awk '{print ($1);}' | cut -d ":" -f3 ---------------------------- But i made it into a script and tried to print the variable, its... (5 Replies)
Discussion started by: meheretoknow
5 Replies

Featured Tech Videos