Remove line breaks and extra spaces

Hi,
I want to remove all extra spaces, line breaks .
Need a new line entry only for term starting"array"

For eg: my input is


array([ 0.03015278,  0.02738889,  0.0125    ,  0.00875   ,  0.00736806,
        0.00672917,  0.00763889,  0.01044444,  0.01178472,  0.01470139,
        0.02020833,  0.03288194,  0.05940278,  0.03489583,  0.01829167,
        0.01255556,  0.00811111,  0.00670139,  0.00543056,  0.00570139,
        0.00616667,  0.00821528,  0.01138889,  0.02573611,  0.03044444,
        0.57640972]), 
array([ 0.03372518,  0.03230408,  0.00867816,  0.00548067,  0.00434692,
        0.00423197,  0.00397074,  0.0049373 ,  0.00669279,  0.01053292,
        0.0149373 ,  0.02205329,  0.03096134,  0.01904389,  0.009279  ,
        0.00683386,  0.00335946,  0.00386102,  0.00250261,  0.0030303 ,
        0.00272727,  0.00426332,  0.00734587,  0.03399164,  0.05005747,
        0.67085162]), 
array([ 0.035     ,  0.03807317,  0.01169512,  0.00759146,  0.00479878,
        0.00514634,  0.0037378 ,  0.00606707,  0.00528049,  0.00892683,
        0.00631707,  0.01397561,  0.01756098,  0.0159939 ,  0.00477439,
        0.00714024,  0.00343293,  0.00530488,  0.00282927,  0.00380488,
        0.00390854,  0.00551829,  0.00696951,  0.03689634,  0.0427439 ,
        0.6965122 ]),

and my expected output is

array([ 0.03015278, 0.02738889, 0.0125 , 0.00875 , 0.00736806, 0.00672917, 0.00763889, 0.01044444, 0.01178472, 0.01470139, 0.02020833, 0.03288194, 0.05940278, 0.03489583, 0.01829167, 0.01255556, 0.00811111, 0.00670139, 0.00543056, 0.00570139, 0.00616667, 0.00821528, 0.01138889, 0.02573611, 0.03044444, 0.57640972]), 
array([ 0.03372518, 0.03230408, 0.00867816, 0.00548067, 0.00434692, 0.00423197, 0.00397074, 0.0049373 , 0.00669279, 0.01053292, 0.0149373 , 0.02205329, 0.03096134, 0.01904389, 0.009279 , 0.00683386, 0.00335946, 0.00386102, 0.00250261, 0.0030303 , 0.00272727, 0.00426332, 0.00734587, 0.03399164, 0.05005747, 0.67085162]), 
array([ 0.035 , 0.03807317, 0.01169512, 0.00759146, 0.00479878, 0.00514634, 0.0037378 , 0.00606707, 0.00528049, 0.00892683, 0.00631707, 0.01397561, 0.01756098, 0.0159939 , 0.00477439, 0.00714024, 0.00343293, 0.00530488, 0.00282927, 0.00380488, 0.00390854, 0.00551829, 0.00696951, 0.03689634, 0.0427439 , 0.6965122 ]),

Is it possible using awk?

How about

paste -s -d"     \n" file | tr -s ' ' ' '
array([ 0.03015278, 0.02738889, 0.0125 , 0.00875 , 0.00736806, 0.00672917, 0.00763889, 0.01044444, 0.01178472, 0.01470139, 0.02020833, 0.03288194, 0.05940278, 0.03489583, 0.0182916
array([ 0.03372518, 0.03230408, 0.00867816, 0.00548067, 0.00434692, 0.00423197, 0.00397074, 0.0049373 , 0.00669279, 0.01053292, 0.0149373 , 0.02205329, 0.03096134, 0.01904389, 0.00
array([ 0.035 , 0.03807317, 0.01169512, 0.00759146, 0.00479878, 0.00514634, 0.0037378 , 0.00606707, 0.00528049, 0.00892683, 0.00631707, 0.01397561, 0.01756098, 0.0159939 , 0.004774
1 Like

And just for nawks...

 awk '{sub("^ *","");s=(s?s " " $0:$0);if($0~".*]), ?$"){print s;s=""}}' file 

tried this on my text file but it gave me this issue:

awk: line 1: regular expression compile failed (missing '(')
.]), ?$

Try again escaping "\)" the right parenthesis.

#!/usr/bin/perl

use warnings;
use 5.18.0;

use IO::File;

my $fh = IO::File->new( "< file.txt" );

undef $/;

my $t = <$fh>;
$t =~ s/\n\s//g;

print $t;

close $fh;
array([ 0.03015278,  0.02738889,  0.0125    ,  0.00875   ,  0.00736806,0.00672917,  0.00763889,  0.01044444,  0.01178472,  0.01470139,0.02020833,  0.03288194,  0.05940278,  0.03489583,  0.01829167,0.01255556,  0.00811111,  0.00670139,  0.00543056,  0.00570139,0.00616667,  0.00821528,  0.01138889,  0.02573611,  0.03044444,0.57640972]),
array([ 0.03372518,  0.03230408,  0.00867816,  0.00548067,  0.00434692,0.00423197,  0.00397074,  0.0049373 ,  0.00669279,  0.01053292,0.0149373 ,  0.02205329,  0.03096134,  0.01904389,  0.009279  ,0.00683386,  0.00335946,  0.00386102,  0.00250261,  0.0030303 ,0.00272727,  0.00426332,  0.00734587,  0.03399164,  0.05005747,0.67085162]),
array([ 0.035     ,  0.03807317,  0.01169512,  0.00759146,  0.00479878,0.00514634,  0.0037378 ,  0.00606707,  0.00528049,  0.00892683,0.00631707,  0.01397561,  0.01756098,  0.0159939 ,  0.00477439,0.00714024,  0.00343293,  0.00530488,  0.00282927,  0.00380488,0.00390854,  0.00551829,  0.00696951,  0.03689634,  0.0427439 ,0.6965122 ]),