Nate F
Nate F

Reputation: 13

split multiple non-delimited numbers to two significant digits

I need to split a long non-delimited series of numbers using awk. How would I split this:

91.5799.3677.48107.5981.26

using awk to produce this:

91.57
99.36
77.48
107.59
81.26

Upvotes: 1

Views: 123

Answers (3)

Jotne
Jotne

Reputation: 41456

Here is an awk as requested..

awk 'NR==1 {s=$1} NR>1 {e=substr($1,1,2);print s "." e;s=substr($1,3)}' RS="." <<< "91.5799.3677.48107.5981.26"
91.57
99.36
77.48
107.59
81.26

Another version

awk '{for (i=1;i<=NF;i++) {if ($i==".") f=i;printf "%s"(f&&i==f+2?RS:""),$i}}' FS=""
91.57
99.36
77.48
107.59
81.26

Upvotes: 1

Dimitre Radoulov
Dimitre Radoulov

Reputation: 28000

If GNU grep is not available and Perl is acceptable:

perl -pe's|\...(?!\Z)|$&\n|g' infile

GNU awk:

awk 'NF&&$0=$0RT' RS='\\...' infile

Upvotes: 1

anubhava
anubhava

Reputation: 785146

It might be easier with egrep:

s='91.5799.3677.48107.5981.26'
egrep -o '[^.]*.[0-9]{2}' <<< "$s"
91.57
99.36
77.48
107.59
81.26

Upvotes: 5

Related Questions