0

I have a text file from a simulation where the numbers are without any spaces between them. An example is given below. I want to separate the numbers based on the decimal point number (13).

5.656854249492415.594870951694315.594870951694315.594870951694321.466252583998021.466252583998021.466252583998027.4809024597083   

I want the output like the following:

5.6568542494924   
15.5948709516943  
15.5948709516943  
15.5948709516943  
21.4662525839980  
21.4662525839980  
21.4662525839980  
27.4809024597083

I have tried to use sed command as below but it is not working.

sed -i 's/\(.\{14\}\)/\1 /g' input.txt

I have attached a screenshot as a reference. enter image description here

I appreciate any helpful suggestions.
Thank you

1
  • You just need to be a bit more precise in your regex: sed -E 's/\.[0-9]{13}/& /g' -- a literal dot followed by 13 digits. Your regex says "14 of any character" Nov 18, 2021 at 1:22

5 Answers 5

2

I think you could be able to just grep this:

$ grep -Eo '[0-9]+\.[0-9]{13}' < input.txt
5.6568542494924
15.5948709516943
15.5948709516943
15.5948709516943
21.4662525839980
21.4662525839980
21.4662525839980
27.4809024597083

This works because it starts looking at the next match at the position the previous one ended, and doesn't look for overlapping matches. You could change that to [0-9]+\.[0-9]{0,13} to allow for shorter numbers too, provided they're separated by nondigits. That also works because the match is greedy, so if there's 13 digits, it'll eat them all for the first number before moving on to the next.

1

You can use this sed (13 characters after the dot):

# with new lines as in the output shown in the question
$ sed 's/\.\([^.]\{13\}\)/&\n/g' file
5.6568542494924
15.5948709516943
15.5948709516943
15.5948709516943
21.4662525839980
21.4662525839980
21.4662525839980
27.4809024597083

Or less verbose GNU sed:

$ sed -E 's/\.[^.]{13}/&\n/g'
1

Using standard (non-GNU) sed (which can't insert a newline character using \n with the s command):

$ sed -e 's/[0-9]\{13\}/&:/g' -e 'y/:/\n/' file
5.6568542494924
15.5948709516943
15.5948709516943
15.5948709516943
21.4662525839980
21.4662525839980
21.4662525839980
27.4809024597083

This inserts a : after every run of 13 digits in the input, and then replaces all those colons with newlines using the y command.

Or, using a literal newline:

sed 's/[0-9]\{13\}/&\
/g' file

Or, if your shell understands $'...' strings:

sed $'s/[0-9]\\{13\\}/&\\\n/g' file
0
echo '5.6568542494924 15.5948709516943 15.594870951694315.594870951694321.466252583998021.466252583998021.466252583998027.4809024597083' |
    sed -nr '{s/ //g;: start;h;s/^([^.]*\..{13}).*$/\1/;p;g;s/^[^.]*\..{13}//; t dummy; : dummy; s/^(..*)$/\1/; t start; }'
5.6568542494924
15.5948709516943
15.5948709516943
15.5948709516943
21.4662525839980
21.4662525839980
21.4662525839980
27.4809024597083

This solution handles spaces in the input.

0
echo '5.6568542494924 15.5948709516943 15.594870951694315.594870951694321.466252583998021.466252583998021.466252583998027.4809024597083' |
    awk -F . '{ printf "%d.",$1; for(i=2;i<NF;i++) { frac=substr($i,1,13); intg=substr($i,14); printf "%s\n%d.",frac,intg; } print $NF;}'
5.6568542494924
15.5948709516943
15.5948709516943
15.5948709516943
21.4662525839980
21.4662525839980
21.4662525839980
27.4809024597083

This solution handles spaces in the input.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .