I need to write a quick (tomorrow's) filter script to replace line breaks (LF or CRLF) found in double quotes with an escaped new line \n . The content is a (broken) javascript program, so I need to enable escape sequences such as "ab\"cd" and "ab\\"cd"ef" inside the line.
I understand that sed is not suitable for work, since it works on a string, so I move on to perl, which I know nothing about :)
I wrote this regular expression: "(((\\.)|[^"\\\n])*\n?)*" And tested it with http://regex.powertoy.org . It really matches the quoted lines with line breaks, however perl -p -e 's/"(((\\.)|[^"\\\n])*(\n)?)*"/TEST/g' does not work.
So my questions are:
- how to make perl to match line breaks?
- how to write the replace-by part to keep the original line and replace only newline?
There is such a similar question with an awk solution, but this is not quite what I need.
NOTE. I usually don’t ask “please do it for me”, but I really don’t want to learn perl / awk tomorrow ... :)
EDIT : example data
"abc\"def" - matches as one string "abc\\"def"xy" - match "abcd\\" and "xy" "ab cd ef" - is replaced by "ab\ncd\nef"
davka source share