Ubuntu: Putting a newline whenever it match third space



Question:

I have a data that looks like this :

 8.620 -9.182 1.758 8.786 -8.249 -0.495 8.614 -9.652 -1.158 9.446 -9.278     0.350 11.450 -7.809 0.887 10.638 -9.000 2.070 11.179 -10.871 -1.200 7.583     -10.330 0.400 7.079 -9.330 -1.014 6.745 -11.702 0.004 11.610 -12.260 -1.101     12.381 -13.783 -   

I want to make three column rows, so whenever it match third space, print new line. Is there some bash command that I can use or how inside the perl script I can change it. This is not a file, I splitted lines according to space, picked up [6,7,8] element and just pushed these elements into an array. When I print array it look unorganized.

This is the part of the script:

if ($file_each[$line]=~/^ATOM/)                  {              $file_each[$line]=~s/^ATOM//;                      @os=split '\s+', $file_each[$line];              chomp @os[6,7,8];              push @coord, @os [6,7,8];              print "@coord\n";              }  

This is how result should look like:

8.620 -9.182 1.758    8.786 -8.249 -0.495    8.614 -9.652 -1.158   


Solution:1

If a string $line contains your numbers, try:

$line =~ s/(.*?\d +.*?\d .*?\d) +/$1\n/g;  

We may try it from a file with

perl -pe 's/(.*?\d +.*?\d .*?\d) +/$1\n/g' ex  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »