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


Views: 11078
turn rows into columns

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


Views: 11070
Re: [iain] turn rows into columns

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


iain
Novice

Feb 11, 2010, 3:59 PM


Views: 11050
Re: [iain] turn rows into columns

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


Views: 11045
Re: [iain] turn rows into columns

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


Views: 11025
Re: [FishMonger] turn rows into columns

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


Views: 11022
Re: [iain] turn rows into columns

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


Views: 11015
Re: [FishMonger] turn rows into columns

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


Views: 11013
Re: [iain] turn rows into columns

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


Views: 11005
Re: [FishMonger] turn rows into columns

thanks alot for all that, it works perfectly!