Aaron Perry Aaron Perry - 5 months ago 8
Linux Question

Insert space between numbers after matching specific pattern

I have the following temperature data that needs to be manipulated. Thought about using

sed
to insert a space in between the data but, the data is changing constantly.

76 75 75 77 80 84 87 91 94 97100101101102101 97 92 89 86 84 82 81 79 78 77


I need it to look like this:

76 75 75 77 80 84 87 91 94 97 100 101 101 102 101 97 92 89 86 84 82 81 79 78 77


Right now, I am using the following code to parse out the highest value (where the numbers are clustered together):

grep -Eo '[0-9]' .../temp.txt | sort -rn | head -n 1

Answer

The first thing to do would be to modify whatever it is that generates the data so that it outputs it with a wider field width.

If that's not possible, then the following will de-mangle the output:

$ sed 's/\([ 1-9][0-9][0-9]\)/ \1/g' data | tr -s ' '
76 75 75 77 80 84 87 91 94 97 100 101 101 102 101 97 92 89 86 84 82 81 79 78 77

I put the tr at the end just to remove consecutive whitespaces in the output from sed (generated for all two-digit integers, except for the first one which, in this case, isn't matched by the regular expression).

But really, the generator of the data will need to be updated.

Comments