CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
turn rows into columns

 



iain
Novice

Feb 11, 2010, 3:40 AM

Post #1 of 9 (6555 views)
turn rows into columns Can't Post

Hello,

Just wondering if anyone can help me.

I have some large files that have a set of numbers in them that i need.

The numbers are layed out in many rows as such:

3.09779100E-02 4.35695193E+00 4.35695193E+00 4.35695193E+00 1.07147124E+00
2.19032046E+00 1.65640707E+00 -4.22757360E-01 -4.22757360E-01 -4.30046280E-01

but i need to make a file with them all in one column,

ie.

3.09779100E-02

4.35695193E+00

4.35695193E+00

4.35695193E+00

1.07147124E+00

2.19032046E+00

1.65640707E+00

-4.22757360E-01

-4.22757360E-01

-4.30046280E-01

but without spaces between lines.

If anyone can help it would be much appreciated because everytime i try to do this it doesn't work.

thanks,

iain


FishMonger
Veteran / Moderator

Feb 11, 2010, 6:38 AM

Post #2 of 9 (6547 views)
Re: [iain] turn rows into columns [In reply to] Can't Post

c:\>perl -pi.bak -e "s/\s+/\n/g" numbers.txt


iain
Novice

Feb 11, 2010, 3:59 PM

Post #3 of 9 (6527 views)
Re: [iain] turn rows into columns [In reply to] Can't Post

Thanks alot for that, it was very useful.

When i process the data i need to remove the spaces formed in the new column.

At the moment it looks like this:

2.19032046E+00
1.65640707E+00
-4.22757360E-01
-4.22757360E-01
-4.30046280E-01

-5.74002450E-01
-5.74002450E-01
1.47363740E+01
-1.49222415E+01
-1.49222415E+01


Do you know how i could delete every 6th line in the file starting from the 6th?

thanks again,
Iain


FishMonger
Veteran / Moderator

Feb 11, 2010, 7:17 PM

Post #4 of 9 (6522 views)
Re: [iain] turn rows into columns [In reply to] Can't Post

The solution I posted would have stripped out all spaces, so where is this new column coming from?

Are you doing some sort of processing on this file after applying the solution I posted?


iain
Novice

Feb 12, 2010, 5:29 AM

Post #5 of 9 (6502 views)
Re: [FishMonger] turn rows into columns [In reply to] Can't Post

I was a bit confused about whether i needed to make a perl file containing the script you gave me. I tried but i couldn't get it to work so i typed:

perl -pi.bak -e "s/\s+/\n/g" numbers.txt

into my unix window which generated a file with all the numbers in a column but with spaces.

Do i need to make a perl script instead?

Sorry if this is all very basic, i am still trying to get to grips with perl.

thanks again


FishMonger
Veteran / Moderator

Feb 12, 2010, 5:45 AM

Post #6 of 9 (6499 views)
Re: [iain] turn rows into columns [In reply to] Can't Post

As shown below, I can't duplicate your problem.

Quote
[root@rkb-2 ~]# cat numbers.txt
3.09779100E-02 4.35695193E+00 4.35695193E+00 4.35695193E+00 1.07147124E+00
2.19032046E+00 1.65640707E+00 -4.22757360E-01 -4.22757360E-01 -4.30046280E-01
[root@rkb-2 ~]# perl -pi.bak -e "s/\s+/\n/g" numbers.txt
[root@rkb-2 ~]# cat numbers.txt
3.09779100E-02
4.35695193E+00
4.35695193E+00
4.35695193E+00
1.07147124E+00
2.19032046E+00
1.65640707E+00
-4.22757360E-01
-4.22757360E-01
-4.30046280E-01


Can you post your 'numbers.txt' file as an attachment so I can see exactly what it contains?


iain
Novice

Feb 12, 2010, 6:46 AM

Post #7 of 9 (6492 views)
Re: [FishMonger] turn rows into columns [In reply to] Can't Post

i'm not sure what is going wrong when i am doing it.

I have attached the input file which is called charges.txt and the output file which is called charges_column.txt. I have shortened them to upload because they were too big. For some reason every 6th line in the column comes out as a blank one.

hope this helps,

thanks
Attachments: charges.txt (3.68 KB)
  charges_column.txt (1.89 KB)


FishMonger
Veteran / Moderator

Feb 12, 2010, 6:56 AM

Post #8 of 9 (6490 views)
Re: [iain] turn rows into columns [In reply to] Can't Post

Each row has leading blank spaces which are then converted to "\n".

There are several ways to strip those leading spaces.

Here's an adjusted version of the previous one I posted.

Code
perl -pi.bak -e "s/^\s+//, s/\s+/\n/g" charges.txt



iain
Novice

Feb 12, 2010, 11:32 AM

Post #9 of 9 (6482 views)
Re: [FishMonger] turn rows into columns [In reply to] Can't Post

thanks alot for all that, it works perfectly!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives