如何在bash脚本中使用awk转义单引号 [英] How to escape single quote using awk in a bash script

查看:504
本文介绍了如何在bash脚本中使用awk转义单引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我到目前为止的尝试,尝试了多种方法,但并不能完全正确. 我的目标是清理输入内容,以防止从文本文件输入到mysql时出现问题

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

cat 'file.txt' | awk '{gsub(/'"'"'/, '.') ; print $0}' > 'file_sanitized.txt'

推荐答案

我不确定问题是否正确.如果要用点替换所有出现的单引号,请使用tr:

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

如果您想使用反斜杠对单引号进行转义,请使用sed,如下所示:

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.

这篇关于如何在bash脚本中使用awk转义单引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆