LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 06-02-2006, 07:14 PM   #1
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 30
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
 
Old 06-02-2006, 08:01 PM   #2
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
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
 
Old 06-02-2006, 09:10 PM   #3
frankie_DJ
Member
 
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Original Poster
Rep: Reputation: 30
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
 
Old 06-02-2006, 09:41 PM   #4
demon_vox
Member
 
Registered: May 2006
Location: Argentina
Distribution: SuSE 10
Posts: 173

Rep: Reputation: 30
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
 
Old 06-03-2006, 07:16 AM   #5
ppanyam
Member
 
Registered: Oct 2004
Location: India
Distribution: Redhat
Posts: 88

Rep: Reputation: 15
In perl the following should work

@data = `cat filename.txt`;

for ($i=1;$i<=$#data;$i+=2)
print $data[$i],"\n";
 
Old 06-03-2006, 07:17 AM   #6
ppanyam
Member
 
Registered: Oct 2004
Location: India
Distribution: Redhat
Posts: 88

Rep: Reputation: 15
By the way can anyone tell me how to use code tags around here?

Thanks
 
Old 06-03-2006, 07:43 AM   #7
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374Reputation: 2374
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reading in data in columns from a file (C++) Nylex Programming 4 03-08-2006 06:55 PM
deleting 100 rows in text file (1-100) via terminal itz2000 Linux - Newbie 5 01-18-2006 12:19 AM
columns & rows Ammad Linux - General 1 08-08-2005 05:02 AM
rows and columns digitalgravy Linux - General 2 03-16-2004 07:47 PM
text mode resolution (changing rows and cols) jaylee Linux - General 3 06-22-2003 09:16 PM


All times are GMT -5. The time now is 03:12 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration