Hi. I've been spending far too much time trying to accomplish a (seemingly) simple thing: use a Perl substitution operation to replace, a comma followed by a space followed by a single double-quote with a single double-quote. The obvious s/, "/"/g doesn't work, nor does escaping the double-quotes, as in s/, \"/\"/g I must have tried dozens of variations. RE's can be tough at times. Late last night I had to admit defeat. Any ideas? Thanks, Doug
Did you try escaping the comma? On 3/1/06, douglas.r.aker@verizon.com <douglas.r.aker@verizon.com> wrote:
Hi.
I've been spending far too much time trying to accomplish a (seemingly) simple thing: use a Perl substitution operation to replace, a comma followed by a space followed by a single double-quote with a single double-quote.
The obvious s/, "/"/g doesn't work, nor does escaping the double-quotes, as in s/, \"/\"/g
I must have tried dozens of variations. RE's can be tough at times. Late last night I had to admit defeat.
Any ideas?
Thanks, Doug _______________________________________________ Wlug mailing list Wlug@mail.wlug.org http://mail.wlug.org/mailman/listinfo/wlug
On Wed, Mar 01, 2006 at 09:30:26AM -0500, douglas.r.aker@verizon.com wrote:
The obvious s/, "/"/g doesn't work, nor does escaping the double-quotes, as in s/, \"/\"/g
Try $target =~ s/,\s\"/\"/g; -- Frank Sweetser fs at wpi.edu | For every problem, there is a solution that WPI Network Engineer | is simple, elegant, and wrong. - HL Mencken GPG fingerprint = 6174 1257 129E 0D21 D8D4 E8A3 8E39 29E3 E2E8 8CEC
Thanks Jared and Frank for your quick responses. The solution, it turns out, had nothing to do with RE's, but rather with input lines and files. I'm dealing with multi-line, comma-separated-value records, using Perl programs to convert them to single line records, change bare carriage-return characters to newline characters and eventually remove extraneous trailing commas, e.g. ...,"Moe, Larry, Curly, ",... (the comma and space after "Curly" is extraneous). I guess you can only do so much in one pass through an input file. The solution was to write two Perl programs, and use a two-stage process as in: cat multi-line-input-records | makethemsinglelines.pl | removetrailingcommas.pl > greatlookingfile.out Thanks again, Doug "Frank Sweetser" <fs@WPI.EDU> Sent by: wlug-bounces@mail.wlug.org 03/01/2006 09:40 AM Please respond to "Worcester Linux Users Group" <wlug@mail.wlug.org> To "Worcester Linux Users Group" <wlug@mail.wlug.org> cc Subject Re: [Wlug] Perl puzzle On Wed, Mar 01, 2006 at 09:30:26AM -0500, douglas.r.aker@verizon.com wrote:
The obvious s/, "/"/g doesn't work, nor does escaping the double-quotes, as in s/, \"/\"/g
Try $target =~ s/,\s\"/\"/g; -- Frank Sweetser fs at wpi.edu | For every problem, there is a solution that WPI Network Engineer | is simple, elegant, and wrong. - HL Mencken GPG fingerprint = 6174 1257 129E 0D21 D8D4 E8A3 8E39 29E3 E2E8 8CEC _______________________________________________ Wlug mailing list Wlug@mail.wlug.org http://mail.wlug.org/mailman/listinfo/wlug
I find that for doing multiple edits on a single line on a single pass its best to use awk. -Jared On 3/1/06, douglas.r.aker@verizon.com <douglas.r.aker@verizon.com> wrote:
Thanks Jared and Frank for your quick responses.
The solution, it turns out, had nothing to do with RE's, but rather with input lines and files.
I'm dealing with multi-line, comma-separated-value records, using Perl programs to convert them to single line records, change bare carriage-return characters to newline characters and eventually remove extraneous trailing commas, e.g. ...,"Moe, Larry, Curly, ",... (the comma and space after "Curly" is extraneous). I guess you can only do so much in one pass through an input file.
The solution was to write two Perl programs, and use a two-stage process as in:
cat multi-line-input-records | makethemsinglelines.pl | removetrailingcommas.pl > greatlookingfile.out
Thanks again, Doug
*"Frank Sweetser" <fs@WPI.EDU>* Sent by: wlug-bounces@mail.wlug.org
03/01/2006 09:40 AM Please respond to "Worcester Linux Users Group" <wlug@mail.wlug.org>
To "Worcester Linux Users Group" <wlug@mail.wlug.org> cc
Subject Re: [Wlug] Perl puzzle
On Wed, Mar 01, 2006 at 09:30:26AM -0500, douglas.r.aker@verizon.comwrote:
The obvious s/, "/"/g doesn't work, nor does escaping the double-quotes, as in s/, \"/\"/g
Try
$target =~ s/,\s\"/\"/g;
-- Frank Sweetser fs at wpi.edu | For every problem, there is a solution that WPI Network Engineer | is simple, elegant, and wrong. - HL Mencken GPG fingerprint = 6174 1257 129E 0D21 D8D4 E8A3 8E39 29E3 E2E8 8CEC _______________________________________________ Wlug mailing list Wlug@mail.wlug.org http://mail.wlug.org/mailman/listinfo/wlug
_______________________________________________ Wlug mailing list Wlug@mail.wlug.org http://mail.wlug.org/mailman/listinfo/wlug
I've been spending far too much time trying to accomplish a (seemingly) simple thing: use a Perl substitution operation to replace, a comma followed by a space followed by a single double-quote with a single double-quote.
Octal escapes are one way: perl -pe 's/, \042/\042/g' See the ASCII(7) man page for octal and hex codes. --Andre __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
Thanks Andre. You know, last night I was looking for a good table showing ascii codes. That ascii man page is a nice tip. Thanks again, Doug "Andre Lehovich" <andrel@yahoo.com> Sent by: wlug-bounces@mail.wlug.org 03/01/2006 10:49 AM Please respond to "Worcester Linux Users Group" <wlug@mail.wlug.org> To "Worcester Linux Users Group" <wlug@mail.wlug.org> cc Subject Re: [Wlug] Perl puzzle
I've been spending far too much time trying to accomplish a (seemingly) simple thing: use a Perl substitution operation to replace, a comma followed by a space followed by a single double-quote with a single double-quote.
Octal escapes are one way: perl -pe 's/, \042/\042/g' See the ASCII(7) man page for octal and hex codes. --Andre __________________________________________________ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com _______________________________________________ Wlug mailing list Wlug@mail.wlug.org http://mail.wlug.org/mailman/listinfo/wlug
On Wed, Mar 01, 2006 at 09:30:26AM -0500, douglas.r.aker@verizon.com wrote:
I've been spending far too much time trying to accomplish a (seemingly) simple thing: use a Perl substitution operation to replace, a comma followed by a space followed by a single double-quote with a single double-quote.
Ok.
The obvious s/, "/"/g doesn't work, nor does escaping the double-quotes, as in s/, \"/\"/g
There's no reason to escape the double-quote, it has no special meaning in perl RE (same with the comma, the space, etc). The first version should work, assuming you're doing the substitution on $_. ie: $ perl -e '$_=q/Foo, "Bar", "Baz"/; s/, "/"/g; print "$_\n"' Foo"Bar""Baz" If it's not something simple like operating on the wrong variable or $_ not containing the data you expect it to, a possible more obscure issue that I've run into problems with is related to a bug in perl (typically 5.8.0) where the double-quote isn't properly dealt with sometimes. If this is the case, the solution I've used is to tell perl to not do Unicode (5.8.0 has a fairly buggy Unicode implementation) by putting "use bytes" at the top of the script. -- Randomly Generated Tagline: "It's not that we're afraid ... Far from it ... It's just that we have this thing about death ... it's not us." - Space Balls
participants (5)
-
Andre Lehovich
-
douglas.r.aker@verizon.com
-
Frank Sweetser
-
Jared Greenwald
-
Theo Van Dinter