regex – sed – 删除大型csv文件中引号内的引号
我正在使用流编辑器sed将大量文本文件数据(400MB)转换为csv格式. 我已经非常接近完成,但突出的问题是引号内的引号,对于这样的数据: 1,word1,"description for word1","another text",""text contains "double quotes" some more text" 2,word2,"description for word2","text may not contain double quotes,but may contain commas," 3,word3,"description for "word3"","more text and more" 所需的输出是: 1,"text contains double quotes some more text" 2,"description for word3","more text and more" 我一直在寻找帮助,但我没有太接近解决方案,我尝试了以下seds与正则表达式模式: sed -i 's/(?<!^s*|,)""(?!,""|s*$)//g' *.txt sed -i 's/(?<=[^,])"(?=[^,])//g' *.txt 这些来自以下问题,但似乎不适用于sed: Related question for perl Related question for SISS 原始文件是* .txt,我正在尝试用sed编辑它们. 解决方法这是使用GNU awk和 FPAT变量的一种方法:gawk 'BEGIN { FPAT="([^,]+)|("[^"]+")"; OFS=","; N=""" } { for (i=1;i<=NF;i++) if ($i ~ /^".*"$/) { gsub(/"/,"",$i); $i=N $i N } }1' file 结果: 1,"text contains double quotes some more text" 2,"another text","more text and more" 说明:
(编辑:4S站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- linux – 模拟麦克风(虚拟麦克风)
- linux – Linked无法找到符号,但读取库并存在符号
- nginx 配置管理 - 简单也复杂
- linux – 在Ubuntu服务器12.04.4上编译Node.js v0.11.12-re
- Linux下的IPv6流量透明代理
- linux – 替换属性文件中的环境变量
- LINUX学习:PXE+DHCP+TFTP+Cobbler 无人值守安装CentOS 7
- LINUX入门:Redhat 7.2 中文显示及中文输入法设置
- 10 Linux DIG Command Examples for DNS Lookup--reference
- linux – 如何在Linix中执行文件操作即cp,mv,rm和chown等时