LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   text data conversion: rows into columns (http://www.linuxquestions.org/questions/programming-9/text-data-conversion-rows-into-columns-451033/)

frankie_DJ 06-02-2006 06:14 PM

text data conversion: rows into columns
 
Hi guys,

I have data in the following format

1 realnumber1 2 realnumber2 3 realnumber3 4 realnumber4 5 realnumber5
6 realnumber6 7 realnumber7 8 realnumber8 9 realnumber9 10 realnumber10
.
.
.
4608 realnumber4608

I would like to convert it to:

1 realnumber1
2 realnumber2
3 realnumber3
...
.
.
.
4608 realnumber4608

or just

realnumber1
realnumber2
.
.
.
realnumber4608

In other words, I would like to convert each line into a column and output them one after another.

I am guessing I should be able to do this in awk, sed or fortran but I don't really know how as I am pretty new to all of them. Can anyone help me solve this problem?

Thanks, Frankie

demon_vox 06-02-2006 07:01 PM

You dont mention what is the actual caracter in between the numbers. So if it just a space you could do:

cat my_data_file.txt | cut -d ' ' -f "2 4 6 8 10" | sed "s/ /\n/g"

if it can have more than one space or tabs you could do:
cat my_data_file.txt | tr "\t" " " | tr -s " " | cut -d ' ' -f "2 4 6 8 10" | sed "s/ /\n/g"

In both case you get just a column with the realnumbers.

I hope this is useful, and it does what you need :)

frankie_DJ 06-02-2006 08:10 PM

thanks for the reply. it makes sense what you did i should have thought of it. but for some reason instead of new line i get this:


n247.443In2nn243.286I
n238.872In7nn237.393I
n235.759In12nn234.007I
n232.299In17nn231.424I
n229.825In22nn228.864I

These are first five lines of my input file:


1 247.443I 2 244.457I 3 243.286I 4 242.492I 5 240.353I
6 238.872I 7 238.120I 8 237.393I 9 236.585I 10 236.320I
11 235.759I 12 235.163I 13 234.007I 14 233.297I 15 233.201I
16 232.299I 17 232.004I 18 231.424I 19 230.982I 20 230.000I
21 229.825I 22 229.417I 23 228.864I 24 228.482I 25 227.870I

demon_vox 06-02-2006 08:41 PM

Mmmm I've tried it with your data and it works fine on my system. But for what I see as your output you might have omitted the inverted slash ( \ ) in the sed command, so insted of getting a newline you are just getting an n.

The numbers are separated by single spaces right?

See if this is the problem or or or or... we'll look for another solution jajajaja

ppanyam 06-03-2006 06:16 AM

In perl the following should work

@data = `cat filename.txt`;

for ($i=1;$i<=$#data;$i+=2)
print $data[$i],"\n";

ppanyam 06-03-2006 06:17 AM

By the way can anyone tell me how to use code tags around here?

Thanks

druuna 06-03-2006 06:43 AM

Hi,

@ppanyam: You could have searched the site for the answer. But here's the link you are looking for:

LQ tags

Hope this helps.


All times are GMT -5. The time now is 02:53 AM.