2

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.
noobuserpm
  • 21
  • 4

4 Answers4

3

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

perl -p0777e "s/('|\x22)/&/g" "e:\output.csv"
Borodin
  • 126,100
  • 9
  • 70
  • 144
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • This worked. Tbf to the other posts, I realized I was checking the logic using the wrong output file, but once I fixed that and I removed all of the escape characters, it was syntactically and logically correct. Thank you so much for saving my last work of 2016! – noobuserpm Dec 29 '16 at 20:47
  • 1
    @Borodin, yes, `\"` is clearer, but you have three parsers (`cmd`, argument parser, regular expression parser) *"fighting"* to deal with quoted data start/end and special characters. In this case, clearer still generates problems. – MC ND Dec 29 '16 at 21:08
1

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"
dbenham
  • 127,446
  • 28
  • 251
  • 390
0

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
Borodin
  • 126,100
  • 9
  • 70
  • 144
  • if ' or " then replace with &. It still isn't replacing single quotes or double quotes in the output. But I'm going to try some of the other suggestions like using hex/octal notation. – noobuserpm Dec 29 '16 at 20:38
  • @noobuserpm: My code replaces single quotes but only *pairs of* double quotes. I've updated it. Does that work for you? – Borodin Dec 29 '16 at 20:47
  • It was trivial for me to try it (yay for scripting) but it did not work. It started streaming from the output file. Which is a lot of text. Not sure what went wrong there, but since I have the answer, I'm going to let this one go. Thanks for trying though. – noobuserpm Dec 29 '16 at 20:50
  • @noobuserpm: I thought it was clear that I had simply omitted the redirection of the output to avoid obfuscating the solution. If you want to send your data to a file then you should write `perl -pe "s/['\"]/^&/g" E:\output.csv > E:\output2.csv` – Borodin Dec 29 '16 at 20:51
  • that's what I ran. I only changed the "search for" field. I kept the I/O options the same as my original. It started streaming to the cmd window which was new behavior. – noobuserpm Dec 29 '16 at 20:57
  • @noobuserpm: Then I suspect that you made a mistake. The only double quotes should be around the substution and the escaped one in the character class. It sounds like you dropped the `>`. – Borodin Dec 29 '16 at 21:11
-1

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!

noobuserpm
  • 21
  • 4