Home > Archive > PERL Beginners > March 2008 > \Q on an pattern containing double quotes, braces etc.
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 |
\Q on an pattern containing double quotes, braces etc.
|
|
| R Chandrasekhar 2008-03-18, 4:02 am |
| Dear Folks,
I want to comment out certain lines in a file that match a particular pattern.
The file contains lines with characters like: {, }, ==, and ". Specifically, I
want to replace lines beginning with
ATTRS{idVendor}=="07b4", ATTRS{idProduct}=="0109",
with
# ATTRS{idVendor}=="07b4", ATTRS{idProduct}=="0109",
I have been unsuccessful in including the pattern as above and have had to work
around using just the two number patterns like so:
--------
#!/usr/bin/perl -i.bak -pl
use warnings;
use strict;
m/07b4/ and m/0109/ and s/$_/# $_/;
--------
This works, but leaves me wondering how I could include the full pattern,
including metacharacters. Using \Q did not help, but perhaps I was doing
something wrong.
I have included some sample lines that can be used as an input file.
--------
ATTRS{idVendor}=="0553", ATTRS{idProduct}=="0202", MODE="0660", GROUP="plugdev"
ATTRS{idVendor}=="06bd", ATTRS{idProduct}=="0403", MODE="0660", GROUP="plugdev"
ATTRS{idVendor}=="07b4", ATTRS{idProduct}=="0109", MODE="0660", GROUP="plugdev"
ATTRS{idVendor}=="06bd", ATTRS{idProduct}=="0404", MODE="0660", GROUP="plugdev"
ATTRS{idVendor}=="04fc", ATTRS{idProduct}=="504b", MODE="0660", GROUP="plugdev"
ATTRS{idVendor}=="07b4", ATTRS{idProduct}=="0100", MODE="0660", GROUP="plugdev"
ATTRS{idVendor}=="07b4", ATTRS{idProduct}=="0105", MODE="0660", GROUP="plugdev"
ATTRS{idVendor}=="07b4", ATTRS{idProduct}=="0114", MODE="0660", GROUP="plugdev"
ATTRS{idVendor}=="07b4", ATTRS{idProduct}=="0114", MODE="0660", GROUP="plugdev"
ATTRS{idVendor}=="07b4", ATTRS{idProduct}=="0109", MODE="0660", GROUP="plugdev"
ATTRS{idVendor}=="07b4", ATTRS{idProduct}=="0105", MODE="0660", GROUP="plugdev"
--------
Thanks.
Chandra
| |
| John W. Krahn 2008-03-18, 4:02 am |
| R (Chandra) Chandrasekhar wrote:
> Dear Folks,
Hello,
> I want to comment out certain lines in a file that match a particular
> pattern. The file contains lines with characters like: {, }, ==, and ".
'{' is only special in a regular expression if it is immediately
followed by a numerical digit, for example '{7}' is special whereas
'{abc}' is not. '}' is not special unless it is part of '{<numerical
digits>}'. '=' and '"' are never special in a regular expression.
> Specifically, I want to replace lines beginning with
>
> ATTRS{idVendor}=="07b4", ATTRS{idProduct}=="0109",
There is nothing in that that needs to be escaped.
> with
>
> # ATTRS{idVendor}=="07b4", ATTRS{idProduct}=="0109",
>
> I have been unsuccessful in including the pattern as above and have had
> to work around using just the two number patterns like so:
>
> --------
> #!/usr/bin/perl -i.bak -pl
> use warnings;
> use strict;
> m/07b4/ and m/0109/ and s/$_/# $_/;
Use s/^/# /; instead of s/$_/# $_/;.
> --------
>
> This works, but leaves me wondering how I could include the full
> pattern, including metacharacters. Using \Q did not help, but perhaps I
> was doing something wrong.
>
> I have included some sample lines that can be used as an input file.
>
> --------
> ATTRS{idVendor}=="0553", ATTRS{idProduct}=="0202", MODE="0660",
> GROUP="plugdev"
$ perl -Mre=debug -wle'my $x = qr/ATTRS{idVendor}=="0553",
ATTRS{idProduct}=="0202", MODE="0660", GROUP="plugdev"/'
Freeing REx: `","'
Compiling REx `ATTRS{idVendor}=="0553", ATTRS{idProduct}=="0202",
MODE="0660", GROUP="plugdev"'
size 22 Got 180 bytes for offset annotations.
first at 1
1: EXACT <ATTRS{idVendor}=="0553", ATTRS{idProduct}=="0202",
MODE="066...>(22)
22: END(0)
anchored "ATTRS{idVendor}=="0553", ATTRS{idProduct}=="0202",
MODE="0660", GROUP="plugdev"" at 0 (checking anchored isall) minlen 79
Offsets: [22]
1[79] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0]
0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 0[0] 80[0]
Freeing REx: `"ATTRS{idVendor}==\"0553\", ATTRS{idProduct}==\"0202\",
MODE"......'
Nope, nothing in there that needs to be escaped.
John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
|
|
|
|
|