Sample Header Ad - 728x90

How to edit a string matching a pattern in a specific field on the condition another string is not present on the same line

0 votes
3 answers
239 views
I need to edit the string "NA" to "Na" only if it is in the 6th field of a file. I can currently achieve this with: awk '{gsub("NA","Na",$6)}1' $filename However I need the additional condition that this wont occur if the string "HEM" is in the same line. For example I would want the following line to stay the same: 7353 NA 5.4433 24.4404 -3.0784 NA 456 HEM Lastly, the above command removes the extra padding between fields. If there is a command that can accomplish this without changing the spacing between fields that would be awesome, but this feature is not necessary. I think this could be done with a while read through each file and printing each line, and only editing with an if statement, but I am trying to avoid something clunky like that. I feel like this one is a tall order to do in a simple fashion, but I would be super appreciative if any unix wizards know how. Thanks!
Asked by CPC_92 (3 rep)
Oct 29, 2024, 11:42 PM
Last activity: Nov 1, 2024, 08:08 PM