UNIX for Beginners Questions & Answers

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

awk, if statement, solved

👤 Login to reply
 
Thread Tools Search this Thread Display Modes
    #1  
Old 3 Weeks Ago
geomarine geomarine is offline
Registered User
 
Join Date: May 2018
Last Activity: 3 July 2018, 4:15 AM EDT
Posts: 13
Thanks: 18
Thanked 1 Time in 1 Post
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

Sponsored Links
    #2  
Old 3 Weeks Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 July 2018, 4:17 PM EDT
Location: Aachen, Germany
Posts: 13,073
Thanks: 449
Thanked 4,013 Times in 3,690 Posts
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 (3 Weeks Ago)
Sponsored Links
    #3  
Old 3 Weeks Ago
geomarine geomarine is offline
Registered User
 
Join Date: May 2018
Last Activity: 3 July 2018, 4:15 AM EDT
Posts: 13
Thanks: 18
Thanked 1 Time in 1 Post
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 3 Weeks Ago
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is online now Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 18 July 2018, 6:12 PM EDT
Location: Foxborough, MA
Posts: 8,312
Thanks: 257
Thanked 839 Times in 783 Posts
Code:
awk '$3 >= -2000 {$3="NaN"}1' myFile

The Following User Says Thank You to vgersh99 For This Useful Post:
geomarine (3 Weeks Ago)
Sponsored Links
    #5  
Old 3 Weeks Ago
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 18 July 2018, 4:17 PM EDT
Location: Aachen, Germany
Posts: 13,073
Thanks: 449
Thanked 4,013 Times in 3,690 Posts
Quote:
Originally Posted by geomarine View Post
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.
Sponsored Links
👤 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
awk to replace a specific field in certain condition mady135 Shell Programming and Scripting 7 11-24-2014 11:02 PM
[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
Comparison of Cells in EXCEL using awk meva Shell Programming and Scripting 19 03-01-2010 04:03 PM



All times are GMT -4. The time now is 06:25 PM.

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





Not a Forum Member?
Forgot Password?