awk replace cells with NaN/delete if condition

Hello, I will like to delete/replace $3 with NaN. condition $3>-2000 (file1.dat) to produce out.dat. I want to retain the structure of the table. I use this code, this output only $3. Any idea on how to modify this code. Thank you.

awk -v OFS='' '{for(i=1; i<=NF; i++) if ($i > -2000 || $i ==" > -2000 ")$i = NaN }1' file1.xyz > out.xyz 
file1.dat
2181.0640997 2898.54726885 -4682.91259766
2182.06412909 2898.54726885 -2683.671875
2183.06415848 2898.54726885 -684.24609375
2184.06418787 2898.54726885 -1684.00146484
2185.06421726 2898.54726885 -2682.8671875
2186.06424665 2898.54726885 -4080.03369141
2187.06427604 2898.54726885 -1675.8359375
2188.06430543 2898.54726885 -3671.14892578
2189.06433482 2898.54726885 -6666.21142578
2190.06436421 2898.54726885 -1661.75146484
2191.0643936 2898.54726885 -4657.61230469
2192.06442299 2898.54726885 -1653.79052734
2193.06445238 2898.54726885 -5650.45507813
2194.06448177 2898.54726885 -4647.76513672
2195.06451116 2898.54726885 -1645.11816406
2196.06454055 2898.54726885 -6642.38427734

out1.dat
2181.0640997 2898.54726885 -4682.91259766
2182.06412909 2898.54726885 -2683.671875
2183.06415848 2898.54726885 -684.24609375
2184.06418787 2898.54726885 NaN
2185.06421726 2898.54726885 -2682.8671875
2186.06424665 2898.54726885 -4080.03369141
2187.06427604 2898.54726885 NaN
2188.06430543 2898.54726885 -3671.14892578
2189.06433482 2898.54726885 -6666.21142578
2190.06436421 2898.54726885 NaN
2191.0643936 2898.54726885 -4657.61230469
2192.06442299 2898.54726885 NaN
2193.06445238 2898.54726885 -5650.45507813
2194.06448177 2898.54726885 -4647.76513672
2195.06451116 2898.54726885 NaN
2196.06454055 2898.54726885 -6642.38427734

What do you mean by "I want to retain the structure of the table"?
Why do you run the for loop across all fields if you want to operate on $3 only?
What are the two or ed branches in the if construct for?
Why is line 3 not NaN ned?

1 Like

Thanks RudiC, I could just remove the fields with values above -2000, but I want to remain the structure such that I have a 4x4 matrix. then perform calculations only on the remaining values.
I made several attempts to operate only on $3, but couldn't so I decided to try another way, since $1 and $2 can't be affected by the condition (>-2000).

---------- Post updated at 03:35 PM ---------- Previous update was at 03:32 PM ----------

I also considered print $3, so I can apply the condition to it separately, but the whole fields are deleted and could not fit the 4x4 structure I intend to retain.

awk '$3 >= -2000 {$3="NaN"}1' myFile
1 Like

Help me out - what 4x4- matrix are you talking of? I see a 3 fields file n lines long. If you need to convert that to another matrix, post the rules / logics to be applied and people in here might be able to help.