develooper Front page | perl.beginners | Postings from March 2008

\Q on an pattern containing double quotes, braces etc.

Thread Next
From:
R Chandrasekhar
Date:
March 17, 2008 22:32
Subject:
\Q on an pattern containing double quotes, braces etc.
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

Thread Next


Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About