3

只是想知道如何在字段周围添加单引号,这样我就可以将其导入 mysql 而不会出现警告或错误。

我有一个包含大量内容的 csv 文件。

16:47:11,3,r-4-VM,250000000.,0.50822578824,131072,0,0,0,0,0

期望的输出

'16:47:07','3','r-4-VM','230000000.','0.466028518635','131072','0','0','0','60','0'

我真的不知道从哪里开始,非常感谢您的帮助。

4

6 回答 6

17

你可以试试这个

awk -F"," -v quote="'" -v OFS="','" '$1=$1 {print quote $0 quote}' file
  1. 将每个分隔符 (, 逗号) 替换为 (',' quote-comma-quote) ->(-F"," -v OFS="','")
  2. 在行首和行尾添加引号 ->(print quote $0 quote)
于 2013-05-10T21:26:41.120 回答
1

try this:

awk '{gsub(/^|$/,"\x027");gsub(/,/,"\x027,\x027")}7' file

example

kent$  echo "16:47:11,3,r-4-VM,250000000.,0.50822578824,131072,0,0,0,0,0"|awk '{gsub(/^|$/,"\x027");gsub(/,/,"\x027,\x027")}7'
'16:47:11','3','r-4-VM','250000000.','0.50822578824','131072','0','0','0','0','0'
于 2013-05-10T21:05:13.383 回答
1

这可能对您有用(GNU sed):

sed -r 's/[^,]+/'\''&'\''/g' file

或者:

sed -r "s/[^,]+/'&'/g" file
于 2013-05-10T21:59:23.900 回答
1
#!/usr/bin/awk -f

BEGIN { FS=OFS=","}

{
    for (i = 1; i <= NF; ++i)
        $i = "'" $i "'"
    print
}

At the beginning, set FS (the field separator) to a comma; also set OFS, the output field separator, to a comma.

For every input line, loop over all fields. NF is the number of fields parsed out of the current line. Set each field to its own value surrounded by single quotes.

When done updating the fields, print the modified line.

于 2013-05-10T21:02:04.187 回答
0
awk 'BEGIN{FS=OFS=","}{for (i=1;i<=NF;++i)  $i="~"$i"~"}{print}' $input_csv_file

这行得通。在这里,我将所有 csv 文件列都用~.

于 2020-05-22T08:11:19.440 回答
0

awk并且sed不会(轻松)确定字段分隔符 ( ,) 是否被转义。csv 文件格式,通过将整个字段括在双引号中来转义字段中的字符(请参阅RFC4180的第 2.6 节)。

正如我在这个答案中所描述的,一种更强大的方法是使用 csv 库,而不是使用正则表达式等解析为文本。

我发现 Python 的库是最好的选择,因为它是:

  1. 广泛可用,没有繁重的依赖,除了 Python 本身;
  2. 对你使用的 Python 版本不是特别敏感;
  3. 适合嵌入到 shell 脚本中;和
  4. 非常紧凑(单线就可以!)。

根据问题的标签,我怀疑这些标准也会吸引你。

因此,请尝试以下操作:

QUOTE_CSV_PY="import sys; import csv; csv.writer(sys.stdout, quoting=csv.QUOTE_ALL, quotechar=\"'\").writerows(csv.reader(sys.stdin))"
python -c "$QUOTE_CSV_PY" < file

分解它:

  • QUOTE_CSV_PY是一个包含 Python 单行命令的 shell 变量
  • Python 命令很简单:
    • 导入标准 sys 和 csv 模块;
    • 创建一个 csv 写入器,它使用 set 写入标准输出 ( stdout),QUOTE_ALL以便所有字段都使用 引用quotechar,它设置为单引号;
    • 为 csv 编写器提供一个从标准输入 ( ) 读取的 csv 阅读器stdin
  • 第二行只是将单行代码传递给 python 解释器,并将 csv 文件(称为file)输入到它的stdin.
于 2020-03-10T14:32:32.433 回答