Background
I have a csv file and i am reading this file in using split.something like this:
while (<>) {
@array = split(/,/, $line);
}
Now the problem is that one of my csv record contains a "," in the record itself. this is confusing the split int breaking the line in the wrong place.
data looks like :
121212, "Simpson, Bart", springfield
I am trying to get it to look like:
121212, Simpson Bart, springfield
Notice Bart Simpson is quoted in the original, that is a function of MS Excel. it adds quotes around any data that is being exported to csv and contains a comma in the data.
I really
can't use modules like :Text::CSV or DBI::CSV. As a mater of fact i can't use any modules. Thus i am trying to use regular expressions.
All i need to do is take out the extra "," comma that is appearing in the the field
Again, I already know that i can do this with some modules but i dont have root to the machine right now and it is not possible for me to install any new modules.
I am trying to do this by using reqular expressions. Here is what i have so far.
open (SYST, "$upload_dir/$filename");
while (defined ($line = <SYST>)) {
next if ($line =~ /^\s+/) ;
if ($line =~ /"(.*)"/) {
print "found line= $line\n";
$line =~ s!
"(.*)"
!
(s/,/sprintf("_", $&))
!ee;
print "now line= $line\n";
}
}
close (SYST);
Here i am searhing if a line contains a "(.*)" (something with quotes around it.
then i am run a sub expression on it that will take what ever is in $1 and run a replace on one character (,) only.
My regular expresion skills are not high and what you see above only needs a slight tweak from what i can tell.
I need to know i can make the secord half work!
Here is a example of what i am trying to do:
line = 121212, "Simpson, Bart", Springfield<br>
if ($line = /"(.*)"/) {
$1 = "Simpson, Bart"; Now contains
# now run replace on Simpson, Bart
$1 =~ s/,/_/g;
$1 = "Simpson_ Bart"; # Now contains
now put everyting back together.
121212, Simpson_ Bart, Sprigfield
}
Thanks in advance!
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.