Rooster
Rooster

Reputation: 10077

which characters must be escaped when running perl -e on the command line

I'm running perl -e on the command line to use perls regex functionality to find and replace a string across many files. However, I've tested my regex in a script as well as many regex testers, but it doesn't seem to work on the command line. So I've begun to wonder if because its the command line I'm needing to escape extra characters. For example, I know that I have to escape $ when I'm using it as a variable, so I thought perhaps I'm needing that in my command. I'm using linux.

heres my command:

perl -pi -w -e 's/"flags" : {[^"]+"CP" : 1[^"]+"prop_name" : "ID"[^:]+: "SKU"/"flags" : {          "SET" : 1       },       "prop_name" : "ID",       "rule" : "SKU+ProductId"/gms;' *_input.xml

I'm trying to match parts of:

   {
      "flags" : {
         "CP" : 1
      },
      "prop_name" : "ID",
      "rule" : "SKU"
   },

so that the inside is changed to:

   {
      "flags" : {
         "SET" : 1
      },
      "prop_name" : "ID",
      "rule" : "SKU+ProductId"
   },

Upvotes: 2

Views: 121

Answers (1)

grebneke
grebneke

Reputation: 4494

Does this work for you? As you are doing a multiline match, but the perl one-liner matches one row at a time, the pattern will fail as soon as you hit newline in your input file.

perl -0777 -pi -w -e 's/"flags" : {[^"]+"CP" : 1[^"]+"prop_name" : "ID"[^:]+: "SKU"/"flags" : {          "SET" : 1       },       "prop_name" : "ID",       "rule" : "SKU+ProductId"/gms;' *_input.xml

Adding -0777 makes perl use the whole file for input. See perlrun for more info.

Upvotes: 1

Related Questions