mass manipulation within a CSV file

mass manipulation within a CSV file

am 01.04.2008 01:50:10 von laredotornado

Hi,

I'm using PHP 5. I have a CSV file, whose absolute path is stored in
$old_file_path. I want to generate a new file that is exactly like
the old one, only that there is an "R-" inserted before the value in
the second column (you can assume the file has at least two columns of
non-empty data). What is the quickest way to do this?

Thanks, - Dave

Re: mass manipulation within a CSV file

am 01.04.2008 04:27:05 von Alexey Kulentsov

laredotornado wrote:
> Hi,
>
> I'm using PHP 5. I have a CSV file, whose absolute path is stored in
> $old_file_path. I want to generate a new file that is exactly like
> the old one, only that there is an "R-" inserted before the value in
> the second column (you can assume the file has at least two columns of
> non-empty data). What is the quickest way to do this?
>
> Thanks, - Dave

1. fast to program

- open input file
- create output file
- while not EOF
- read string using fgetcsv()
- add 'R-' where you want
- write string to output file using fputcsv()
- close files

see http://www.php.net/manual/en/function.fgetcsv.php for details. Here
are lot of examples in comments.

2. fast to run

- read entire file into string using file_get_contents()
- run preg_replace() on string adding 'R-' to second field
- write string to output file using file_put_contents()

Main problem here is to write good regexp dealing with double quotes
right.

http://en.wikipedia.org/wiki/Comma-separated_values
http://www.google.com/search?q=csv+parse+regexp

Re: mass manipulation within a CSV file

am 01.04.2008 05:12:54 von Jerry Stuckle

laredotornado wrote:
> Hi,
>
> I'm using PHP 5. I have a CSV file, whose absolute path is stored in
> $old_file_path. I want to generate a new file that is exactly like
> the old one, only that there is an "R-" inserted before the value in
> the second column (you can assume the file has at least two columns of
> non-empty data). What is the quickest way to do this?
>
> Thanks, - Dave
>

In this case I wouldn't even worry about it being a .csv file. I'd just
read it a line at a time (with fgets()), parse the line, insert the
value where I need it and write it back out.

Or, if this is a one-time occurrence, read it into a spreadsheet, create
a quick macro to do the conversion you wish, and save the file. I
wouldn't even use PHP (or any other programming language).

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: mass manipulation within a CSV file

am 02.04.2008 18:00:28 von Janwillem Borleffs

laredotornado schreef:
> I'm using PHP 5. I have a CSV file, whose absolute path is stored in
> $old_file_path. I want to generate a new file that is exactly like
> the old one, only that there is an "R-" inserted before the value in
> the second column (you can assume the file has at least two columns of
> non-empty data). What is the quickest way to do this?
>

If you are working on a *nix environment and this is for one time use
only, simply use sed:

$ cat test.csv
foo,bar,uu,too
foo,bar,uu,too
foo,bar,uu,too
foo,bar,uu,too
foo,bar,uu,too
foo,bar,uu,too
foo,bar,uu,too
foo,bar,uu,too
foo,bar,uu,too
foo,bar,uu,too
foo,bar,uu,too

$ sed -E 's/,([^,]+)/,R-\1/' ./test.csv >> ./test-new.csv
foo,R-bar,uu,too
foo,R-bar,uu,too
foo,R-bar,uu,too
foo,R-bar,uu,too
foo,R-bar,uu,too
foo,R-bar,uu,too
foo,R-bar,uu,too
foo,R-bar,uu,too
foo,R-bar,uu,too
foo,R-bar,uu,too
foo,R-bar,uu,too



JW