ordering a data file

With an input file like this:

How can I get an output like this?

(In the quoted examples, the "_" sign represents an empty space)

Note that there are some minus signs and no spaces, in the example above the first character of the first line is an empty space, so each number spans 10 characters.
With the command cut -c1-10 I can get the correct numbers but only the first column of the input file, and I don't know how to reorder the numbers.
Any suggestions?

One way:

sed 's/\(.\{10\}\)\(.\{10\}\)\(.\{10\}\)\(.\{10\}\)/\1\n\2\n\3\n\4/' file

It worked perfectly.
Thanks Franklin52!

Hi Franklin,

It will be great if you explain your sed command execution.

sed 's/\(.\{10\}\)\(.\{10\}\)\(.\{10\}\)\(.\{10\}\)/\1\n\2\n\3\n\4/' file

The command has 4 patterns of ten characters:

\(.\{10\}\)

and prints a newline between each pattern:

\1\n\2\n\3\n\4

If Franklin52's solution fails for anyone trying it, it's because the \n escape sequence in replacement text is an extension not supported by all implementations. If your implementation (like mine) does not support it, you need to use a backslash immediately prior to a newline to get the newline in there. Just mentioning it in case someone finds themselves scratching their head :wink:

$ sed 's/\(.\{10\}\)\(.\{10\}\)\(.\{10\}\)\(.\{10\}\)/\1\n\2\n\3\n\4/' data
_1.012E+01n_2.102E+04n_3.222E+03n_4.100E+02
-1.144E+02n-2.489E-02n_5.444E+01n-3.122E-03
-3.566E+00n_7.455E+04n-4.326E+03n_7.834E+00

$ sed -e 's/\(.\{10\}\)\(.\{10\}\)\(.\{10\}\)\(.\{10\}\)/\1\      
> \2\
> \3\
> \4/' data
_1.012E+01
_2.102E+04
_3.222E+03
_4.100E+02
-1.144E+02
-2.489E-02
_5.444E+01
-3.122E-03
-3.566E+00
_7.455E+04
-4.326E+03
_7.834E+00

A slightly different approach:

sed 's/\(.\{10\}\)/\1\
/g; s/\n$//' data

Have a nice weekend,
Alister

awk '{for (i=1;i<=NF;i++) $i=((i-1)%10?X:"\n") $i} {printf $0}' FS="" OFS="" urfile

And what can I do if the file is like this one?

i.e:

I need the output in a single column without empty lines, I tried with the scripts you suggested me something goes wrong.

I need this:

Can anybody help me?

use below for general cases as yours:-

awk '{for (i=1;i<=length($0);i+=10) {s=substr($0,i,10) ; print s} }' infile.txt

:cool::cool::cool:

thanks! really useful!

better one. which will work if the element length is not equal to 10 as well (variable length of elements).

perl -wple 's/([^E])([-_])/$1\n$2/g;'  infile.txt

:cool::cool::cool:

another question (I hope my last one):

I have a file like this:

where zn is some number.
but I need it in the following order:

I hope I have explained clearly

using perl:-

perl -wlne 'push @a,[ substr($_,-1,1),$_];END{$,="\n" ;print map {$_->[1]} sort {$a->[0] <=> $b->[0]}  @a}' infile.txt

;);):wink:

solved! i did:

let lc=1
while [ $lc -lt 100 ]; do
awk 'NR%100==lc' lc=$lc input >> output
let lc=$lc+1
done

I didn't get it !?!?!?

---------- Post updated at 20:36 ---------- Previous update was at 20:22 ----------

your code does not do anything it just print the lines as it is...so what is your
point here???????

your code didn't work for me, but mine did.

I don't understand your perl code :confused:

I created a sample file:

but your code didn't work, mine did. Remember that I need this output:

Instead of invoking awk 99 times this should be a better approach :

awk '{ print; z=substr($0,1,1); n=substr($0,2)
  for(i=10;i<1000;i+=10){
    printf("%s%d%s\n", z, i, n)
  }
}' input > output

Regards