LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 09-19-2008, 03:25 AM   #16
jonnymorris
LQ Newbie
 
Registered: Sep 2008
Posts: 10

Original Poster
Rep: Reputation: 0

Quote:
Originally Posted by jan61 View Post
Moin,

I'd assume you have a syntax error at another part of your script. Did you try the script without the END section?

Jan
The script works fine with the original END loop provided by PTrenholme. Tinkering with it, it appears that when I moved the code around I inadvertently removed some brackets, so the culprit was this part of the line:
Code:
(Record[i,"Name"]!="" ? ", " Record[i,"Name"] : "")
Silly me!

Now it runs, but it seems to give me ALL of the fields instead of just the ones I want ($1, $9 split, $10, $11, $12, $13, $14, $15), the Address array element seems to hold the entire rest of the record. Hmm. Not really sure why this is happening. I will continue to tinker.

EDIT: Ah, I think I see why Address contains so much data. The original input file specification has changed, it contains many fields per record but it is still just the fields I mentioned ($1, $9 - 15) that I want, the rest can be ignored. Just noting this as an update to the original problem.

Last edited by jonnymorris; 09-19-2008 at 04:18 AM.
 
Old 09-19-2008, 06:14 AM   #17
234107
LQ Newbie
 
Registered: Sep 2008
Posts: 8

Rep: Reputation: 0
Perl is better

Code:
#!/usr/bin/perl

$csv_file=shift;
open(DAT, $csv_file) || die("Could not open $csv_file!");
@raw_data=<DAT>;

foreach $csv_record (@raw_data)
{
 chomp($csv_record);
  ($f_name,$l_name,$age)=split(/,/,$csv_record);
  print "$f_name\t$l_name\t$age\n";
};

close(DAT);
Example comma separated file. If you want to change it... lets say not comma, but semicolon, just replace it in the split function like this

Code:
($f_name,$l_name,$age)=split(/;/,$csv_record);
Remember to use backslash, if you are going to use dot, slash or some other special symbol.

Code:
($f_name,$l_name,$age)=split(/\\/,$csv_record);
One backslash for backslash and one backslash for escape character.

Code:
John,Smith,23
Eva,Eva,18
No,Namer,35
Usage:
$perl_file_name_it_as_you_wish.pl mufile.csv
 
  


Reply

Tags
csv, file, manipulation, text


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Comparing two csv files and write different record in third CSV file irfanb146 Linux - Newbie 3 06-30-2008 09:15 PM
separating a comma delimited line mrobertson Programming 7 07-27-2005 01:56 PM
Sed(?); Appending a comma-delineated file ice_hockey Linux - General 2 05-27-2005 08:42 AM
Comma-Delimited Website Filenames Apocalypse General 1 11-09-2003 09:05 AM
comma delimited file cdragon Programming 5 06-21-2002 07:55 PM


All times are GMT -5. The time now is 04:56 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration