I've read the other posts and somehow the solutions don't work.

perl -p0777e "s/('|"")/^&/g" "E:\output.csv" > "E:\output2.csv"

I've tried

perl -p0777e "s/('|^")/^&/g" "E:\output.csv" > "E:\output2.csv"
perl -p0777e "s/('|\")/^&/g" "E:\output.csv" > "E:\output2.csv"

I tried putting all of the other possible funny characters with escape characters with no success. I know that it's the " character that is the issue because I'm running variations of this perl command in the same FOR loop.

This is what the error message says for the top most syntax:

Bareword found where operator expected at -e line 1, near "s/('|")/^&/g E"
syntax error at -e line 1, near "s/('|")/^&/g E"
Execution of -e aborted due to compilation errors.

I can't tell if the E is included because of proximity or because of the " confusion. I can experiment with that.

Can anyone help me figure out how to pass this S&R command safely? Thank you in advance.

EDIT: I can't even comment correctly...

Latest attempt is

perl -p0777e "s/^(^'^|^"^)/^&/g" "E^:\output.csv" > "E:\output2.csv"

with no success. Error message is

Bareword found where operator expected at -e line 1, near "s/^(^'^|^)/&/g E"
syntax error at -e line 1, near "s/^(^'^|^)/&/g E" Execution of -e aborted
due to compilation errors.
4

There are 4 best solutions below

2
On BEST ANSWER

The easiest way to handle it is to remove the problematic quote character

perl -p0777e "s/('|\x22)/&/g" "e:\output.csv"
6
On

You don't need anything so convoluted as that

But what are you trying to do? This converts all single quotes ' or pairs of double quotes "" to a single ampersand &

perl -0777 -pe "s/('|\"\")/&/g" E:\output.csv

If you want to convert just single or double quotes to ampersands then it's even easier

perl -0777 -pe "s/('|\")/^&/g" E:\output.csv

or, using a character class

perl -0777 -pe "s/['\"]/^&/g" E:\output.csv

But why are you slurping the whole file? Line by line is fine, like this

perl -pe "s/['\"]/^&/g" E:\output.csv
0
On

You need to worry about escaping/quoting characters for both cmd.exe and perl.exe, and the rules are different.

cmd.exe

Poison characters like &, |, etc. must be either quoted as "&", or escaped as ^& if they are to be interpreted as literals by cmd.exe.

Double quotes are a state machine - the first unescaped quote turns quoting on, and the next one turns it off. A quote can be escaped as ^" to prevent quoting semantics from turning on, but once on, a quote cannot be escaped to prevent it from turning quoting off.

If there are an odd number of quotes, then one of them must be escaped, else the quote semantics will continue past the last quote.

perl.exe

The double quote literal must be escaped as \"

combined

If a quote needs to be escaped for both cmd.exe and perl.exe, you must remember that the cmd.exe escape is applied first. So the caret must go directly in front of the quote like \^". After cmd.exe removes the caret escape, perl only sees \".

So, using the information above, any of the following should work.

perl -p0777e s/('^|\^")/^&/g "E:\output.csv" > "E:\output2.csv"
perl -p0777e "s/('|\")/^&/g^" "E:\output.csv" > "E:\output2.csv"
perl -p0777e ^"s/('^|\")/&/g" "E:\output.csv" > "E:\output2.csv"

You should not require the parentheses

perl -p0777e s/'^|\^"/^&/g "E:\output.csv" > "E:\output2.csv"
perl -p0777e "s/'|\"/^&/g^" "E:\output.csv" > "E:\output2.csv"
perl -p0777e ^"s/'^|\"/&/g" "E:\output.csv" > "E:\output2.csv"

Another alternative is to use a character class instead of alternation

perl -p0777e s/['\^"]/^&/g "E:\output.csv" > "E:\output2.csv"
perl -p0777e ^"s/['\"]/&/g" "E:\output.csv" > "E:\output2.csv"
1
On

So here is the ugliness that solved this problem. At least without a syntax error. However, the S&R logic did not carry through and I will have to remove the parts that break the logic.

perl -p0777e ""s^/^(^'^|""^)^/^&^/g"" "E:\output.csv" > "E:\output2.csv"

Back into the salt mines after lunch...

Thank you for trying @SAM-6!