Inserting single delimeter with spacemacs / smartparens - emacs

I'm editing clojure in spacemacs, and I want to insert an escaped quote mark inside of a string, like this
" \" "
But when I have this in spacemacs, where c indicates where my cursor is
" \c "
and type ", it produces
" \"\" "
I have no idea why it does that. It would also be nice to know in the general case, how to insert a single delimeter like ( incase it's necessary without smartparens auto-pairing it and then having to delete a character.

Related

Postgresql copy CSV format, double quote escape not working

Running into an issue with copying the following data into a DB
1, ab\"c
I receive an unterminated quote error when running the following SQL
copy table_name from sample.tsv CSV DELIMITER ',' QUOTE '"' ESCAPE E'\\'
Based on the postgresql documentation I expect the escape parameter to be used to escape the quotation character but it's not working. Would like to see if there's a solution to this issue without reformatting the data, or changing the quote character.
try this. Because if quote is ", then it will mix with double quote in (ab"c).
copy table_name from 'sample.tsv' (FORMAT CSV, QUOTE '''', DELIMITER ',',ESCAPE E'\\');
It is expecting to find escaped quotes only inside quotes, so the command you show would work for 1,"ab\"c" but not for what you have.
The command that would work for the data you show is:
copy table_name from sample.tsv DELIMITER ','
But it is not likely to work for the rest of your data.

How can I reformat escape characters ready to use as Insert Value for MariaDb?

I'm using Powershell with ODBC to transfer table data between Sage 50 and MariaDb.
Some data rows in a text column in Sage can contain both single and double quotes that need to be retained when the data is imported into MariaDb.
I'm struggling to get PowerShell to replace for the Values portion of the Insert statement:
I need to replace a single quote ' with backslash single quote \', and also a double quote " with backslash double quote \"
For anyone else searching for this the answer is :
For single quotes
[regex]::replace($text, "'", "\'")
For double quotes
[regex]::replace($DETAILS, "`"", "\'")
I found this URL helpful https://vwiki.co.uk/MySQL_and_PowerShell and as stated you may wish to put this in a function.

How can I input a string into a text file after a certain number of characters in perl?

I have a long sql query that I am attempting to put into VBA for Excel. VBA for Excel has a limit to the amount of text that can go on a line and it seems to be about 1000 chars. What I want to do is copy the query to a text file and run it through a perl script and output to the text file with it formatted the way I need it for VBA.
I need Perl to count chars to 1000 then write (" & _) then a line break then (") then repeat the process till the end of the file. Spaces or type of char do not matter. Any help is greatly appreciated. I will check back frequently to see if anyone needs more information. THANKS!!!
A one-liner:
perl -lape 's/(.{1000})(?=.)/$1" &_\n"/g;' < input > output
If you have the text already in a string, one way to do this would be:
$string =~ s/(.{1000})/$1" & _\n"/sg;
print '"', $string, '"';
or, perhaps better:
my #chunks = unpack '(A1000)*', $string;
s/\"/""/g, s/\n/" & vbCrLf & _\n"/g for #chunks; # escape special characters
print '"', join(qq(" & _\n"), #chunks), '"';
If you're reading the input from a file, it's also possible to do this without reading all the input into memory, by setting the input record separator:
{
local $/ = \1000; # read input in chunks of 1000 chars
print '"';
while ( <> ) {
s/\"/""/g; s/\n/" & vbCrLf & _\n"/g; # escape special characters
print $_, qq(" & _\n");
}
print '"';
}
(Some of these methods can sometimes leave a pointless "" on the last line — in fact, the last method will always do that — but I assume that shouldn't be a problem.)
Finally, note that neither of these methods will do anything to escape any special characters (like double quotes) that might appear in the input. If your input might contain such characters, you'll need to deal with them separately. I've marked the points where you could insert code to do that, if you need to.
Edit: I did some Googling, and it looks like the main characters the need to be escaped in double quoted VBA strings are double quotes (which need to be doubled) and newlines (which have no simple encoding, and need to be handled with a kluge like " & vbCrLf & "). I've edited the code above to implement such escaping.
I suppose, if one wanted to be extra sure, one could also escape all non-printable and non-ASCII characters with something like:
s/([^\n -~])/sprintf '" & Chr(%d) & "', ord $1/eg
(Ps. The backslash in s/\"/""/g is there only to avoid confusing SO's syntax highlighter.)

Replacing text with apostrophe text via sed in applescript

I have an applescript to find and replace a number of strings. I ran in the problem of having a replacement string which contained & some time ago, but could get around it by putting \& in the replacement property list. However an apostrophe seems to be far more annoying.
Using a single apostrophe just gets ignored (replacement doesn't contain it), using \' gives a syntax error (Expected “"” but found unknown token.) and using \' gets ignored again. (You can keep doing that btw, even number gets ignored uneven gets syntax error)
I tried replacing the apostrophe in the actual sed command with double quotes (sed "s…" instead of sed 's…'), which works in the command line, but gives a syntax error in the script (Expected end of line, etc. but found identifier.)
The single quotes mess with the shell, the double quotes with applescript.
I also tried '\'' as was suggested here and '"'"' from here.
Basic script to get the type of errors:
set findList to "Thats.nice"
set replaceList to "That's nice"
set fileName to "Thats.nice.whatever"
set resultFile to do shell script "echo " & fileName & " | sed 's/" & findList & "/" & replaceList & " /'"
Try:
set findList to "Thats.nice"
set replaceList to "That's nice"
set fileName to "Thats.nice.whatever"
set resultFile to do shell script "echo " & quoted form of fileName & " | sed \"s/Thats.nice/That\\'s nice/\""
or to stick to your example:
set findList to "Thats.nice"
set replaceList to "That's nice"
set fileName to "Thats.nice.whatever"
set resultFile to do shell script "echo " & quoted form of fileName & " | sed \"s/" & findList & "/" & replaceList & "/\""
Explanation:
The sed statement is usually enclosed by single quotes like this:
set myText to "Hello"
set xxx to do shell script "echo " & quoted form of myText & " | sed 's/ello/i/'"
However, in this example you could have exluded the single quotes altogether.
set myText to "Hello"
set xxx to do shell script "echo " & quoted form of myText & " | sed s/ello/i/"
The unquoted sed statement will break down as soon a space is included.
set myText to "Hello"
set xxx to do shell script "echo " & quoted form of myText & " | sed s/ello/i there/"
--> error "sed: 1: \"s/ello/i\": unterminated substitute in regular expression" number 1
Since you can't include an apostrophe within a single quoted statement (even if you escape it), you can enclose the sed statement in double quotes like this:
set myText to "Johns script"
set xxx to do shell script "echo " & quoted form of myText & " | sed \"s/ns/n's/\""
EDIT
Lauri Ranta makes a good point that if your find or replace string contains escaped double quotes my answer won't work. Her solution is as follows:
set findList to "John's"
set replaceList to "\"Lauri's\""
set fileName to "John's script"
set resultFile to do shell script "echo " & quoted form of fileName & " | sed s/" & quoted form of findList & "/" & quoted form of replaceList & "/"
I'd also use text item delimiters. You don't have to include AppleScript's in the default scope or set the property back if it isn't used later.
set input to "aasearch"
set text item delimiters to "search"
set ti to text items of input
set text item delimiters to "replace"
ti as text
There's no easy way to escape the search or replace patterns if they can contain something that would be interpreted by sed.
set input to "a[a"
set search to "[a"
set replace to "b"
do shell script "sed s/" & quoted form of search & "/" & quoted form of replace & "/g <<< " & quoted form of input
If you have to use regular expressions, scripting languages like Ruby have methods for creating patterns from strings.
set input to "aac"
set search to "(a+)"
set replace to "\\1b"
do shell script "ruby -KUe 'print STDIN.read.chomp.gsub(Regexp.new(ARGV[0]), ARGV[1])' " & quoted form of search & " " & quoted form of replace & " <<< " & quoted form of input without altering line endings

Parsekit: how to match individual quote characters?

When using the parser Parsekit for the iPhone. Is it possible to include against a double quote? And things which are part of the special BNF?
(Is it possible to escape sequences in a defined grammer?)
#start = doublequote+;
doublequote= '"'
Developer of ParseKit here.
By default you can match against quoted strings easily using the built-in QuotedString parser (which will match QuotedString tokens):
#start = quotes;
quotes = QuotedString+;
that would match input like: "foo" 'bar' "baz"
as three quoted strings: "foo", 'bar', "baz"
So this demonstrates that by default the ParseKit tokenizer (the PKTokenizer class) produces QuotedString tokens when encountering a " or '.
For more details on default tokenizer behavior, read the ParseKit tokenization documentation.
However, if you want quote chars (", ') to be recognized as standalone symbols rather than indicating the start or end of a quoted string, you must alter the tokenizer behavior first.
In code, you would alter tokenizer behavior by calling methods on your PKTokenizer object.
In grammars, you alter tokenizer behavior with tokenizer directives.
Tokenizer directives are special rules placed at the top of your grammar which start with a # character. In this case, you want to change which characters are recognized as standalone symbol tokens by the tokenizer. Specifically, you want to add two chars as symbols with the #symbolState tokenizer directive.
You can do that in your grammar by changing it to:
#symbolState = '"' "'"; // a tokenizer directive stating ' and " should be recognized as standalone symbol tokens
// (by default they are start- and end-markers for quoted string tokens)
#start = stuff;
stuff = (Word | Symbol)+;
Given the same input as above, you would match separate quote symbols and words: ", foo, ", ', bar, ', ", baz, "