Home > Archive > PERL Beginners > June 2007 > Help parsing a CSV file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Help parsing a CSV file
|
|
| Mihir Kamdar 2007-06-24, 6:59 pm |
| Hi,
I have a csv file having 3rd field as phone number series. In that field,
some of the records are phone number ranges like 097611/4
Now I need to seperate this into 4 numbers and store them one after the
other, like:-
097611
097612
097613
097614
There are more than 1000 records in the csv file and for each of them, I
want to store as above.
Thanks in advance for your help.
Mihir
| |
| Rodrick Brown 2007-06-24, 6:59 pm |
| On 6/24/07, Mihir Kamdar <kamdarmihir06@gmail.com> wrote:
> Hi,
>
> I have a csv file having 3rd field as phone number series. In that field,
> some of the records are phone number ranges like 097611/4
>
> Now I need to seperate this into 4 numbers and store them one after the
> other, like:-
>
> 097611
> 097612
> 097613
> 097614
>
> There are more than 1000 records in the csv file and for each of them, I
> want to store as above.
>
> Thanks in advance for your help.
> Mihir
>
Did you even read before you post why cant people clearly explain what
exactly they want anymore???
--
Rodrick R. Brown
http://www.rodrickbrown.com
| |
| Tom Phoenix 2007-06-24, 6:59 pm |
| On 6/24/07, Mihir Kamdar <kamdarmihir06@gmail.com> wrote:
> I have a csv file having 3rd field as phone number series. In that field,
> some of the records are phone number ranges like 097611/4
>
> Now I need to seperate this into 4 numbers and store them one after the
> other, like:-
>
> 097611
> 097612
> 097613
> 097614
I recommend writing a Perl program to do this processing. Perl is
excellent at text handling in general, and there are many modules on
CPAN to help with the thornier tasks. Have you tried Perl yet? How far
have you gotten? Where are you stuck?
Cheers!
--Tom Phoenix
Stonehenge Perl Training
| |
| Mihir Kamdar 2007-06-25, 9:59 pm |
| Hi Tom,
I found a script which does this for me which is as follows:-
#!/usr/bin/perl
#open(INFILE,'testJohor1.csv');
#open(OUTFILE,'xyz1.txt');
if (2 != ($#ARGV+1)) {
print "Usage: $0 <infile> <outfile>\n";
exit 1;
}
open INFILE, "<$ARGV[0]" || die "unable to open INFILE";
open OUTFILE, ">$ARGV[1]" || die "unable to open OUTFILE";
while (<INFILE> ) {
if (/(.*)(\d)\/(\d)/) {
my $stub = $1;
my $start = $2;
my $end = $3;
for $i ($start..$end) {
print OUTFILE "$stub$i\n";
}
} else {
print OUTFILE "$_";
}
}
close INFILE;
close OUTFILE;
It converts the fields in my input file like 097611/4 to
097611
097612
097613
097614
But there are some of the fields like 09778/0, which should be converted to
09778
09779
09770
The script ignores these values where difit before '/' is greater than the
one which is after that.
Any improvizations??
Thanks,
Mihir
On 6/24/07, Tom Phoenix <tom@stonehenge.com> wrote:
>
> On 6/24/07, Mihir Kamdar <kamdarmihir06@gmail.com> wrote:
>
> field,
>
> I recommend writing a Perl program to do this processing. Perl is
> excellent at text handling in general, and there are many modules on
> CPAN to help with the thornier tasks. Have you tried Perl yet? How far
> have you gotten? Where are you stuck?
>
> Cheers!
>
> --Tom Phoenix
> Stonehenge Perl Training
>
| |
| Tom Phoenix 2007-06-25, 9:59 pm |
| On 6/25/07, Mihir Kamdar <kamdarmihir06@gmail.com> wrote:
> if (2 != ($#ARGV+1)) {
That works, but it's usually written more like this:
if (@ARGV != 2) {
> open INFILE, "<$ARGV[0]" || die "unable to open INFILE";
> open OUTFILE, ">$ARGV[1]" || die "unable to open OUTFILE";
These don't do what they look like. The vertical-bar-or operator is
high precedence, so the string sticks too tightly to the die, and so
the open will never die. Either put parentheses around the part to the
left of the vertical-bar-or operator, or change to the low-precedence
word 'or' operator. See the precedence chart in the perlop manpage.
> It converts the fields in my input file like 097611/4 to
> 097611
> 097612
> 097613
> 097614
>
> But there are some of the fields like 09778/0, which should be converted to
> 09778
> 09779
> 09770
So the 0 is a special case. Is that last one supposed to be 09770 or 09780?
You can check for 0 and handle it separately. If $end isn't 0, you get
what you need from ($start..$end) . If it is 0, your list of suffixes
would be ($start..9, 0) instead. If you need 09780, you might need to
go with something like this:
my $range;
if ($end) {
@range = "$stub$start".."$stub$end";
} else {
my $stub1 = $stub;
$stub1++; # magical autoincrement
@range = "$stub$start".."$stub1$end";
}
Good luck with it!
--Tom Phoenix
Stonehenge Perl Training
|
|
|
|
|