[Solved] Uneven column to row conversion


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers [Solved] Uneven column to row conversion
# 1  
Old 03-05-2014
[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:
Code:
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:
Code:
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:
Code:
A B 
1 3 
2 4

to
Code:
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
Code:
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
Code:
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

Last edited by Leander; 03-05-2014 at 09:36 AM..
# 2  
Old 03-05-2014
Try this:
Code:
awk '$1=$1' RS= file

This User Gave Thanks to Franklin52 For This Post:
# 3  
Old 03-05-2014
This is exactly what i was looking for Smilie .
Thank you for the fast and simple solution, everything works fine now. Thank you!
# 4  
Old 03-05-2014
Quote:
Originally Posted by Leander
This is exactly what i was looking for Smilie .
Thank you for the fast and simple solution, everything works fine now. Thank you!
You're welcome.
# 5  
Old 03-05-2014
I just noticed on more thing.
Despite awk doing what it should.

For two values
i have an input like
Code:
0.0000000E+00
1
2
3
4

0.0000000E+00
1
2
3
4

However upon using
Code:
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)

Last edited by Franklin52; 03-05-2014 at 11:18 AM.. Reason: Please use code tags
# 6  
Old 03-05-2014
Right, if the value = 0 awk considers the condition as false, try this:
Code:
awk '$1=$1;1' RS= file

# 7  
Old 03-05-2014
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:
Code:
0.47
1
2
3
4
5

0.000E+00
1
2
3
4
5

0.47
1
2
3
4
5

now becomes
Code:
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:
Code:
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.

Last edited by Franklin52; 03-05-2014 at 05:56 PM.. Reason: Please use code tags
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print row on 4th column to all row

Dear All, I have input : SEG901 5173 9005 5740 SEG902 5227 5284 SEG903 5284 5346 SEG904 5346 9010 SEG905 5400 5456 SEG906 5456 5511 SEG907 5511 9011 SEG908 5572 9015 SEG909 5622 9020 SEG910 5678 5739 SEG911 5739 5796 SEG912 5796 9025 ... (3 Replies)
Discussion started by: attila
3 Replies

2. Shell Programming and Scripting

[Solved] Convert Row To column

Hi Folks, I am using db2 command -> db2 list tablespace show detail Tablespace ID = 10 Name = TSCDDHLMSUM Type = Database managed space Contents = All permanent data.... (5 Replies)
Discussion started by: ckwan
5 Replies

3. Emergency UNIX and Linux Support

[Solved] Mysql - Take data from row and copy it to another row

Sorry if I repost my question in this section, but I'm really in a hurry since I have to finish my work... :( Dear community, I have a table with two rows like: Row1 Row2 ======= ======= 7,3 text 1 1,3 text 2 1,2,3 blabla What i need to do is add/copy... (2 Replies)
Discussion started by: Lord Spectre
2 Replies

4. Shell Programming and Scripting

Row to Column conversion?

I have a text file with the geneIds separated by space in each line. The number Ids in lines are different. The file is like: abc qwe tyu ghj jkl dfg sdf cvb sdk fgh tyu uio iop tyu rty eru wer rty iop asd sdf dfg fgh zxc I want to format the file like: abc qwe tyu ghj jkl ... (7 Replies)
Discussion started by: sammy777
7 Replies

5. UNIX for Dummies Questions & Answers

awk to print first row with forth column and last row with fifth column in each file

file with this content awk 'NR==1 {print $4} && NR==2 {print $5}' file The error is shown with syntax error; what can be done (4 Replies)
Discussion started by: cdfd123
4 Replies

6. Shell Programming and Scripting

Subtracting each row from the first row in a single column file using awk

Hi Friends, I have a single column data like below. 1 2 3 4 5 I need the output like below. 0 1 2 3 4 where each row (including first row) subtracting from first row and the result should print below like the way shown in output file. Thanks Sid (11 Replies)
Discussion started by: ks_reddy
11 Replies

7. Shell Programming and Scripting

Moving data from a specified column/row to another column/row

Hello, I have an input file like the following: 11_3_4 2_1_35 3_15__ _16989 Where '_' is a space. The data is in a table. Is there a way for the program to prompt the user for x1,y1 and x2,y2, where x1,y1 is the desired number (for example x=6 y=4 is a value of 4) and move to a desired spot... (2 Replies)
Discussion started by: jl487
2 Replies

8. Shell Programming and Scripting

column to row conversion with additional pattern

Hi there, I've an input file1 as follows: 1001 1002 1003 1004 1005 I would like to have an output file2 as follows: Numbers are 1001/ 1002/ 1003/ 1004/ 1005/ Any help is appreciated. (2 Replies)
Discussion started by: kbirde
2 Replies

9. Shell Programming and Scripting

Changing the column for a row in a text file and adding another row

Hi, I want to write a shell script which increments a particular column in a row from a text file and then adds another row below the current row with the incremented value . For Eg . if the input file has a row : abc xyz lmn 89 lm nk o p I would like the script to create something like... (9 Replies)
Discussion started by: aYankeeFan
9 Replies
Login or Register to Ask a Question