Home Man
Search
Today's Posts
Register

If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

awk replace cells with NaN/delete if condition

Tags
awk, if statement, solved

Login to Reply

 
Thread Tools Search this Thread
# 1  
Old 06-22-2018
Question 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.

Code:
awk -v OFS='' '{for(i=1; i<=NF; i++) if ($i > -2000 || $i ==" > -2000 ")$i = NaN }1' file1.xyz > out.xyz

Code:
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

# 2  
Old 06-22-2018
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 ored branches in the if construct for?
Why is line 3 not NaNned?
The Following User Says Thank You to RudiC For This Useful Post:
geomarine (06-23-2018)
# 3  
Old 06-22-2018
Question

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.
# 4  
Old 06-22-2018
Code:
awk '$3 >= -2000 {$3="NaN"}1' myFile

The Following User Says Thank You to vgersh99 For This Useful Post:
geomarine (06-23-2018)
# 5  
Old 06-23-2018
Quote:
Originally Posted by geomarine
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.

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.
Login to Reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Delete complete row according to condition jiam912 Shell Programming and Scripting 10 08-17-2014 04:11 PM
Delete File in a Directory Using a Condition shekhar2010us Shell Programming and Scripting 8 09-03-2013 06:32 PM
Replace with condition radius UNIX for Dummies Questions & Answers 3 05-17-2013 11:29 AM
Delete if condition met in a column jazzyzha Shell Programming and Scripting 9 04-08-2013 06:01 AM
Delete records within a file upon a condition jacobs.smith Shell Programming and Scripting 1 02-28-2013 05:12 PM
Need to delete all lines where any line meets a condition dbiggied Shell Programming and Scripting 7 10-24-2012 12:59 PM
Delete records from a big file based on some condition hemantraijain UNIX for Dummies Questions & Answers 1 09-25-2012 02:41 AM
[Solved] Delete files with condition awadeid UNIX for Dummies Questions & Answers 5 05-31-2012 11:56 AM
How do you delete cells from a space delimited text file given row and column number? evelibertine UNIX for Dummies Questions & Answers 5 06-01-2011 01:41 PM
Delete line with sed or awk (with specified condition) </kida> Shell Programming and Scripting 4 01-17-2011 04:24 PM


All times are GMT -4. The time now is 08:44 PM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password