我想在目录中添加一个充满文件的双引号。转型如下:
INSERT INTO %T (colA, colB)
更改为INSERT INTO %T ("colA", "colB")
我尝试过使用sed
的各种示例。我能够grep
识别文件和行号。你能告诉我如何在不手动修改文件的情况下实现这个目的吗?
答案 0 :(得分:1)
这可能会:
awk -F"[()]" '{n=split($(NF-1),a,", *");for (i=1;i<n;i++) s=s sprintf("\"%s\", ",a[i]);s=s "\""a[n]"\"";print $1 "("s")"}'
EKS:
echo 'INSERT INTO %T (colA, colB, colC)' | awk -F"[()]" '{n=split($(NF-1),a,", *");for (i=1;i<n;i++) s=s sprintf("\"%s\", ",a[i]);s=s "\""a[n]"\"";print $1 "("s")"}'
INSERT INTO %T ("colA", "colB", "colC")
答案 1 :(得分:1)
sed '
# Filter to insert only
/INSERT INTO/ !b
# Add first and last " inside ()
s/(/("/
s/)[^)]*$/")/
# add all internal " around each ,
s/,[[:space:]]*/","/g
' YourFile
在线版本(没有过滤器并假设空格字符为空格):
sed 's/(/("/;s/)[^)]*$/")/;s/, */","/g' YourFile
()
且只有一组()
答案 2 :(得分:0)
使用awk的另一种方法
awk -F"(" -vOFS="(" '{gsub(/[^, )]+/,"\"&\"",$2)}1' file
INSERT INTO %T ("colA", "colB")