Solved

Writing at the begining of the .csv file

Posted on 2007-03-30
10
184 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

920 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now