I have a file such as this:
ME45 P 1311 41130 1.253
ME39 P 1311 41130 7.700
ME38 P 1311 41130 7.776
ME37 P 1311 41130 8.285
ME36 P 1311 41130 8.689
ME30 P 1311 4113010.252
ME26 P 1311 4113010.486
ME29 P 1311 41130 9.598
ME28 P 1311 41130 9.356
ME21 P 1311 41130 9.911
ME20 P 1311 4113010.465
ME17 P 1311 4113010.984
and I need to replace the space between two immediate adjacent numbers with zero (e.g. replace the gap between the second column where there is 1131
and the third column where there is 411
with 0
), which will return me the desired output such as:
KALI P 131104113008.580
IMOB P 131104113001.863
When I say the space between two immediate adjacent numbers, meaning there is only one space between two number and I want to replace this space with zero.
So far, I have been using awk
to try and solve this:
awk '{gsub("1311 41130", "1311041130")}1' myfile > myfile_tmp && mv myfile_tmp myfile
but unfortunately, the file contains thousands of lines and as the series of numbers changes, it becomes painful to look each block of column one by one.
My idea to solve this is by iterating over a series of strings, store them in a variable or an array, check if an element containing blank space exists and return its index, then check whether neighbouring element is a number or not by using this "blank space" index as a reference, and then replace this space with zero if indeed it has numeric neighbours. However, I don't know if it is doable in bash
or awk
. I have a better understanding of Python, but somehow this blank space is a hurdle to me; Python might recognise this space as a delimiter.
Is there any way to solve this problem elegantly?