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
XScreenSaver(1) 					      General Commands Manual						   XScreenSaver(1)

NAME
truchet - draws curved or angular Truchet patterns SYNOPSIS
truchet [-display host:display.screen] [-foreground color] [-background color] [-window] [-root] [-mono] [-install] [-visual visual] [-delay microseconds] [-min-width integer] [-min-height integer] [-max-width integer] [-max-height integer] [-max-linewidth integer] [-min- linewidth integer] [-erase] [-no-erase] [-erase-count integer] [-square] [-not-square] [-curves] [-no-curves] [-angle] [-no-angles] [-scroll] [-scroll-overlap integer] [-anim-delay integer] [-anim-step-size integer] DESCRIPTION
The truchet program draws arc and line based Truchet patterns. OPTIONS
accepts the following options: -window Draw on a newly-created window. This is the default. -root Draw on the root window. -mono If on a color display, pretend we're on a monochrome display. -install Install a private colormap for the window. -visual visual Specify which visual to use. Legal values are the name of a visual class, or the id number (decimal or hex) of a specific visual. -delay microseconds How long to wait between drawing each screenful. Default is 3 seconds. -min-width integer The minimum width in pixels of each square. Default is 40. -min-height integer The minimum height in pixels of each square. Default is 40. -max-width integer The maximum width in pixels of each square. Default is 150. -max-height integer The maximum height in pixels of each square. Default is 150. -max-linewidth integer The maximum width of the lines used to draw. Default is 25. -min-linewidth integer The minimum width of the lines used to draw. Default is 2. -erase -no-erase Whether to clear the screen after each screenful is drawn. Default is True (erase). -erase-count integer The number of screenfuls to draw before erasing. Default is 25. -square -not-square Whether to force the tiles to be square. Defualt is False (not-square). -curves -no-curves Whether to draw the arc based Truchet pattern. Default is True (curves). -angles -no-angles Whether or not to draw the line based Truchet pattern. Default is True (angles) -scroll Use the scroll mode. Default is False. -scroll-overlap The amount to scroll from one side to another. Default is 400. -anim-delay The time to pause between each animation step. Default is 100. -anim-step-size The amount of steps to skip between each animation step. Default is 3. ENVIRONMENT
DISPLAY to get the default host and display number. XENVIRONMENT to get the name of a resource file that overrides the global resources stored in the RESOURCE_MANAGER property. SEE ALSO
X(1), xscreensaver(1) COPYRIGHT
Copyright (C) 1998 by Adrian Likins. Permission to use, copy, modify, and distribute this software and its documentation for any purpose and without fee is hereby granted, pro- vided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in sup- porting documentation. AUTHOR
Adrian Likins, 1998. X Version 11 27-May-97 XScreenSaver(1)
All times are GMT -4. The time now is 10:24 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy