Solved

Writing at the begining of the .csv file

Posted on 2007-03-30
10
188 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
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…

679 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