
|
View Full Version : Export a text file to CSV format.
steveks 11-03-2008, 02:01 PM Export a text file to CSV format.
Hello Gurus,
I have a text file with the following format. I need a script or something like that which converts the text file in to a CSV format.
"A" status"unavailable"
"B" status"available"
"C" status"unavailable"
"D" status"unavailable"
"E" status"available"
"F" status"available"
Thanks in advance for your help guys.
__________________
The man who removes a mountain begins by carrying away small stones.
dspkable 11-03-2008, 02:18 PM Have your tried importing the file with something like Excel and then exporting or saving it from there as a CSV file? If it doesn't have a "," in the text file, the maybe a find and replace the "spaces" with a "comma space" might do it too.
__________________- Donovan K Dedicated Servers | VPS | Shared Hosting | Colocation | Reseller Plans | Monthly SpecialsOnly New Hardware | Only 24/7 Support | Only at JADASE.com
steveks 11-03-2008, 02:51 PM I am not sure on what you are telling. This is the output of a script. I need to export this output in to CSV format. Any help is great.
__________________
The man who removes a mountain begins by carrying away small stones.
steveks 11-03-2008, 03:36 PM Guys, please help me.. Looking for your help more..
__________________
The man who removes a mountain begins by carrying away small stones.
awatson 11-03-2008, 06:13 PM If it's the output of a script, then you'll need to make the change within the script. Not sure anyone can help with that without seeing the script.
__________________www.NEXCESS.NET - Shared/Reseller Hostingwww.eliteRAX.com - Dedicated Servers, Server Clusters
etogre 11-03-2008, 07:35 PM I'm at work so this is untested
PHP Code:
<?php
function custom_csv($file, $csv_line_end = '\n')
{
テつ*テつ*テつ*$handle = fopen($file, "r");
テつ*テつ*テつ*テつ*$contents = fread($handle, filesize($file));
テつ*テつ*テつ*テつ*$eaches = explode('\n', $contents);
テつ*テつ*テつ*テつ*$csv_arr = array();
テつ*テつ*テつ*テつ*
テつ*テつ*テつ*テつ*$replace = array(' status', '"', 'unavailable', 'available');
テつ*テつ*テつ*テつ*foreach ($eaches as $each)
テつ*テつ*テつ*テつ*{
テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*$csv_arr[]['status'] = (strpos($each, 'unavailable')) ? 'unavailable' : 'available';
テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*str_replace($replace, '', $each);
テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*$csv_arr[]['name'] = trim($each);
テつ*テつ*テつ*テつ*}
テつ*テつ*テつ*テつ*$csv_str = '';
テつ*テつ*テつ*テつ*foreach ($csv_arr as $csv)
テつ*テつ*テつ*テつ*テつ*テつ*テつ*テつ*$csv_str .= $csv['name'] . ',' .$csv['status'] . $csv_line_end;
テつ*テつ*テつ*テつ*return $csv_str;
}
echo custom_csv('text_file.txt', ';'); // A,available;B,unavailable;etc
?>
Last edited by etogre : 11-03-2008 at 06:42 PM.
CyberCucina 11-04-2008, 02:10 AM Text Editor Solution
If the output is in a file, just about any text editor would allow you to change the space to a comma.
bigfan 11-04-2008, 07:36 AM This should also work:
PHP Code:
$data = file($in_file);$fp = fopen($csv_file, 'w');foreach ($data as $line) {テつ*テつ*テつ*$arr = explode('"', trim($line, "\"\r\n"));テつ*テつ*テつ*テつ*fputcsv($fp, array($arr[0], $arr[2]));}fclose($fp);
Last edited by bigfan : 11-04-2008 at 06:40 AM.
Burhan 11-05-2008, 02:24 AM From the linux command line:
Code:
burhan@burhan-laptop:~$ cat t.txt
"A" status"unavailable"
"B" status"available"
"C" status"unavailable"
burhan@burhan-laptop:~$ replace 'status"' 'status","' -- t.txt
t.txt converted
burhan@burhan-laptop:~$ cat t.txt
"A" status","unavailable"
"B" status","available"
"C" status","unavailable"
burhan@burhan-laptop:~$
OR
Code:
burhan@burhan-laptop:~$ cat t.txt | sed "s/status\"/status\",\"/"
"A" status","unavailable"
"B" status","available"
"C" status","unavailable"
__________________
In order to understand recursion, one must first understand recursion.
If you feel like it, you can read my blog
Signal > Noise
bsddaemon 11-05-2008, 05:47 AM I dont know CSV format (only used in Windows?), but sounds like the OP wants something like this:
Code:
awk -F \" '{print $2", "$4}' < file.txt
A, unavailable
B, available
C, unavailable
D, unavailable
E, available
F, available
__________________
...and the God created man...
bigfan 11-05-2008, 10:03 AM Quote:
only used in Windows?
From wikipedia:
Quote:
Comma-separated value lists are very old technology and predate personal computers by more than a decade; the IBM Fortran (level G) compiler under OS/360 supported these in 1967, and they were not a new idea at the time.
...
The CSV file format is very simple and supported by almost all spreadsheets and database management systems. Many programming languages have libraries available that support CSV files. Even modern software applications support CSV imports and/or exports because the format is so widely recognized.
|