Search code examples
perlbatch-fileescapingsyntax-errorbareword

using a windows batch file/command line that runs a perl s/("|')// command, how to do prevent a bareword found where operator expected error?


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.

Solution

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

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