Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Writing at the begining of the .csv file

Posted on 2007-03-30
10
187 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

Title # Comments Views Activity
Parse csv file and generate graphs in HTML in bash 8 235
batch script for automated email 12 121
Regex rule to match two different url 5 74
Perl Snippet to Parse String 1 22
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 …
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…

856 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