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

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to