This is what I have so far, tried multiple ways but can\'t get it just right. My goal is to sanitize the input to prevent problems while inputting to mysql from text file
<What you want is:
awk '{gsub(/\047/,".")}1' file
See http://awk.freeshell.org/PrintASingleQuote.
You have used wrong quoting:
awk '{gsub(/'"'"'/, "."); print}'
Since you requested "how to escape single quote using awk" here's a solution using awk:
awk '{ gsub("\x27", ".");print $0}'
where \x27
is the escape sequence representation of the hexadecimal value 27 (a single quote).
For a list of all escape sequences see https://www.gnu.org/software/gawk/manual/html_node/Escape-Sequences.html
I'm not sure if I got the problem correctly. If you want to replace all occurrences of a single quote by a dot, use tr
:
tr "'" "." < file.txt > sanitized.txt
If you want to escape the single quote with a backslash use sed
like this:
sed "s/'/\\\'/g" file.txt > sanitized.txt
Note: Please take the advice from CharlesDuffy seriously. This is far from a stable and safe solution to escape values for an SQL import.