Sponsored Content
Top Forums Shell Programming and Scripting Awk: Assigning a variable to be the value of FNR at a certain line Post 302961872 by ThomasP on Saturday 5th of December 2015 09:12:27 AM
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.
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
times(1)							   User Commands							  times(1)

NAME
times - shell built-in function to report time usages of the current shell SYNOPSIS
sh times ksh times DESCRIPTION
sh Print the accumulated user and system times for processes run from the shell. ksh Print the accumulated user and system times for the shell and for processes run from the shell. On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a variable assignment. This means that tilde substitution is performed after the = sign and word splitting and file name genera- tion are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
ksh(1), sh(1), time(1), attributes(5) SunOS 5.11 15 Apr 1994 times(1)
All times are GMT -4. The time now is 07:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy