Solved

Writing at the begining of the .csv file

Posted on 2007-03-30
10
186 Views
Last Modified: 2010-03-05
I have a .csv file in following format:

111111111,F_NAME1 ,L_NAME1,ADDR1,CITY1,MS,12121,787878787,1234567,1234567890
222222222,F_NAME2 ,L_NAME2,ADDR2,CITY2,TN,98765,121212121,98767890,2020202020
333333333,F_NAME3 ,L_NAME3,ADDR3,CITY3,TN,10101,454545454,56565656,1010101010
444444444,F_NAME4 ,L_NAME4,ADDR4,CITY4,TN,12345,987612234,12312312,8989898989

Each row represents one record. I need to write some $result at the begining of each row as one more field/column.
The value of $result will be different for each row, so I know I need to do something like - foreach to go to the next row
but don't know how to do it.

Please help!
0
Comment
Question by:MPJD
10 Comments
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18824703
open(CSV,">path_to_your_file");
while ($line = <CSV>)
{
$line = $result.$line;
print CSV $line;
}
but i did not sure that symbol '>' is for read write or for write only
0
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18824711
I will check it
0
 
LVL 6

Expert Comment

by:nitinsawhney
ID: 18824750
Here is another version

open(CSVIN,"<path_to_your_file"); # Read original file
open(CSVOUT,">path_to_output_file"); # Write new file
while ($line = <CSVIN>)
{
$line = $result.$line;
print CSVOUT $line;
}

Also, if you would like keep the same name as the original file then in the end use system to move the newly created file to old one.

system('mv path_to_output_file path_to_your_file')

Cheers
Nitin
0
Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18824754
Ok
the symbol '+<' is for read/write
open CSV, "+<path_to_your_file";
while ($line = <CSV>)
{
$line = $result.$line;
print CSV $line;
}
close CSV
0
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18824798
if you used nitinsawhney's option dont forget to close the file descriptors

open CSVIN,"<path_to_your_file"; # Read original file
open CSVOUT,">path_to_output_file"; # Write new file
while ($line = <CSVIN>)
{
$line = $result.$line;
print CSVOUT $line;
}
close CSVIN;
close CSVOUT;
system('mv path_to_output_file path_to_your_file')
0
 
LVL 17

Expert Comment

by:mjcoyne
ID: 18826956
This:

open(CSV,">path_to_your_file");
while ($line = <CSV>)
{
$line = $result.$line;
print CSV $line;
}

wil clobber the existing file -- don't try this!  nitinsawhney's second version id OK.  Also, Perl will close the filehandles for you; there's no need to do so explicitly (in this case).  There's no harm if you do it, though.
0
 
LVL 1

Author Comment

by:MPJD
ID: 18837216
thanks everybody!

but when I am doing -
$line=$result.$line,

$result is replacing the 1st column from the file like -

if the file has $line = c1,c2,c3 then after I do
$line = $result.$line, it shows me -

$result,c2,c3

I don't want to replace c1 by $result, I want to write $result before c1 so that it should look like -
$result,c1,c2,c3

I appreciate your help!
0
 
LVL 1

Author Comment

by:MPJD
ID: 18837264
SORRY!! I take back my words,
 its indeed working when I put $result.','.$line
So please ignore my last comment, I am so stupid!
0
 
LVL 4

Expert Comment

by:mukhtar2t
ID: 18837271
No problem at all
0
 
LVL 4

Accepted Solution

by:
mukhtar2t earned 500 total points
ID: 18837299
I actualy missed the comma as you said the code must be :
open CSV, "+<path_to_your_file";
while ($line = <CSV>)
{
$line = $result.','.$line;
print CSV $line;
}
close CSV;

same as you said on your last post

0

Featured Post

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question