[Solved] Uneven column to row conversion

Hi Unix Forum,

I have a relatively easy question i suppose for which, however, until now i could not find a solution.

I am working with a program that will give me an output file similar to the following:

A
1
2
3
4

B
1
2
3
4

C
1
2
...

As the file is rather long and may variy in length for different files
i need to convert it to the following form:

A 1 2 3 4 ...
B 1 2 3 4 ... 

so that ich can use gnuplot.
So far i have only found solutions for cases like:

A B 
1 3 
2 4

to

A 1 2
B 3 4

done with awk.
I sadly am not very familiar with awk or sed commands but i can at least make them work in a simple bash script.

I was hoping for a solution that would do for 1 file something similar to wenn i would have 2 files

A
1
2

and

B 
1 
2 

and paste them into one file 

A  B 
1  1
2  2

Is there any decent solution for this problem that i could use ?

Notice that the number of lines below A might vary in length.
So far i could only think of somehow scanning from the beginning to the first empty line, then write a file A.dat, continue scanning from this empty line (since B has in reality a similar value to A) to the next line, write B.dat and so on.

Then in the end i could paste all those files togehter in a bashscript, still i would not know how to script something like

paste fileA fileB ......fileXXX > total.dat

I appologize for the long description. I am not used to using forums for questions but i really would appreciate some help or
tips for tutorials that might prove helpful for this kind of problem.

Alternatively it would of course also be appropriate if it was possible to plot
this format with gnuplot directly though i doubt that.

Greetings
Leander

Try this:

awk '$1=$1' RS= file

This is exactly what i was looking for :slight_smile: .
Thank you for the fast and simple solution, everything works fine now. Thank you!

You're welcome.

I just noticed on more thing.
Despite awk doing what it should.

For two values
i have an input like

0.0000000E+00
1
2
3
4

0.0000000E+00
1
2
3
4

However upon using

awk '$1=$1' RAW > new

Those 2 lines are (together with its values 1 2 3 4) not written to the new file. They are somehow skipped.

How come awk is ignoring lines with 0.000 ?

Since as is changed it manualy to 0.000001 it works (i'd like not to do this manually though every time, despite the fact that i need the line with 0.000)

Right, if the value = 0 awk considers the condition as false, try this:

awk '$1=$1;1' RS= file

This does indeed work for the zeros, those are now correctly printed.

However the rest of the values have now all doubled.
I tried it also on an example:

0.47
1
2
3
4
5

0.000E+00
1
2
3
4
5

0.47
1
2
3
4
5

now becomes

0.47 1 2 3 4 5
0.47 1 2 3 4 5
0.000E+00 1 2 3 4 5
0.47 1 2 3 4 5
0.47 1 2 3 4 5

How to avoid that ?

It may now be relevant to the question but how
can i for example add a minus in front of the lower half of the values eg:

0.47 1 2 3
0.000E+00 1 2 3
-0.47  1 2 3

Please only answer this one as you see it appropriate.

awk '{$1=$1}1' RS= file

Thank you this simple modification saves some unhandy commands on which i now have wasted my time :confused: .
Thanks nevertheless i'm sure this will be helpfull for the future.

I guess this topic can be closed .

Thanks Franklin! That was an excellent command