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
# 8  
Old 12-06-2015
Maybe if you post some more details we could find a way to help you better. Right now, you're working on two adjacent lines, each pair 10 lines apart. Must be a tribute to your simulation output? The nomenclature used doesn't help either.
A (reduced set of) sample data might help to interpret/find out what you really need...
# 9  
Old 12-08-2015
First of all thanks for your help guys, sorry for my brief absence I had moved on from this to work on something else (also for my thesis).

Good news: the sprintf() did exactly what I needed it to do ! Smilie

So my problem is solved.

For the sake of completeness though I'll try to again explain my problem, how it was solved and I'll attach some data as a sample although it's quite messy (hence the awk processing) but it might be usefull.

Context:
I'm getting about 20 000+ lines per output file, every 11 lines is 'one set of data'.
The simulation works in timesteps, where every X seconds (10-7 seconds in my case) it will calculate what the particles should do next, output data and then repeat the process on the next timestep.
This awk script only needs a few fields, namely $1,$5 and $6 and only every 11th line and every line before that.

The output is generated by LAMMPS an open source modeling software package, in this case I'm using it to perform a granular simulation of two identical particles colliding with each other.

(from here on out, my translation from Dutch to English might be lacking in certain scientific terms but I'll try to make it as clear as possible so bear with me)

In order to perform parametric studies (or scoping studies) I'm comparing how long it takes for the particles to reach a constant energy (this means the collision is over) and how much energy is left.
On top of that I want to determine the distance the particles overlap during the collision simulation and at what point they reach max overlap.


So back to awk.

Each particle's properties are outputted on a separate line.
This required me to compare values from one set of 11 lines to the next, that's what the if(NR%11==0) is for, so every 11th line is being compared to the next and also every line before the 11th to the next, hence the if( (NR +1)%11==0).

When constant energy has been reached I use the exit command to avoid unnecessary computing (in this case it's barely milliseconds that I save in computing time but i'll soon be up-scaling this to simulations that take days to run).

Here comes the problem.

Initially I stored the NFR value of the maximum overlap point in a variable simply like so:

Code:
overlapPoint=NFR

Further along the input file when constant energy was reached, I printed this variable along with the others I had calculated but it turned out to have changed.
That is to say, when I used the printf() to print 'overlapPoint' it printed the current value of NFR, in other words it reevaluated the 'overlapPoint' variable before printing it.

Using:
Code:
overlapPoint=sprintf(NFR)

however, awk did not reevaluate the 'overlapPoint' variable, since the reference to the internal NFR variable is apparently lost thanks to the sprintf() command.

Which solved the problem for me Smilie

If anyone has any suggestions to make my title clearer I'll gladly edit it if I can.
# 10  
Old 12-08-2015
I'm not sure I understand what you are striving for, plus there are some inconsistencies in your spec (NFR variable? change variable?) Howsoever, looking at your sample file, I see it has the time step in each record, and also an indicator for the data you nedd. I tried to simplify your code snippet and came up with this:
Code:
awk '
BEGIN           {printf "Change,Et1,Et2,ColDt,dt,steps,max,maxtm\n"
                }

/ITEM: TIMESTEP/        {getline TMSTP
                        }

/ITEM: ATOMS/   {getline
                 te1 = $5
                 z1  = $1

                 getline
                 te2 = $5
                 dt  = $6
                 ol = 0.05 + z1 - $1
                 if (ol > max)  {max = ol               # overlap calculations
                                 maxtm = TMSTP
                                }
                 if (oldte == te2)
                        {colDt = dt * TMSTP
                         printf("%s,%s,%s,%1.8f,%s,%s,%1.8f,%d\n",change,te1,te2,colDt,dt,TMSTP,max,maxtm)
                         exit
                        }
                 oldte = $5
                }

' file
Change,Et1,Et2,ColDt,dt,steps,max,maxtm
,6.468976533,6.554952976,0.00000150,0.000000100,15,0.00004660,7

which is pretty similar to what your code yields (except for the TIMESTEP which is one less as it starts with 0). Please make sure the DOS line terminators (<CR>, 0x0D) are removed from the file before you try it.
# 11  
Old 12-08-2015
My bad I should've marked this as solved
Login or Register to Ask a Question

Previous Thread | Next Thread

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
Login or Register to Ask a Question