sed将双引号csv逗号分隔转换为非引号管道分隔

sed将双引号csv逗号分隔转换为非引号管道分隔,csv,sed,double-quotes,Csv,Sed,Double Quotes,输入文件“input.file”是: "col one",,,"col, two",,"col, three" ,"col one",,"col, two",,"col, three" ,,"col one","col, two",,"col, three" 所需的输出文件是: col one|||col, two||col, three |col one||col, two||col, three ||col one|col, two||col, three 这就是目前为止的情况: sed

输入文件“input.file”是:

"col one",,,"col, two",,"col, three"
,"col one",,"col, two",,"col, three"
,,"col one","col, two",,"col, three"
所需的输出文件是:

col one|||col, two||col, three
|col one||col, two||col, three
||col one|col, two||col, three
这就是目前为止的情况:

sed -r 's/"([^"])/\1/g; s/,/|/g'  ./input.file
目标是第一次替换

s/"([^"])/\1/g
将解析由“定义的任意字段,并将它们复制到输出和第二个替换

s/,/|/g
将双引号字段中的“,”替换为“|”

$ cat ip.txt 
"col one",,,"col, two",,"col, three"
,"col one",,"col, two",,"col, three"
,,"col one","col, two",,"col, three"

$ perl -pe 's/"[^"]+"(*SKIP)(*F)|,/|/g; s/"//g' ip.txt
col one|||col, two||col, three
|col one||col, two||col, three
||col one|col, two||col, three
  • “[^”]+”(*跳过)(*F)
    跳过模式
    “[^”]+”
    并查找提供的任何其他替代匹配项
    • (*F)
      (*FAIL)
      的缩写,也可以使用
      (?!)
  • |,
    要匹配的备选模式
  • |/g
    将所有
    替换为
    |
  • s///g
    然后删除所有

进一步阅读:


perl解决方案行吗?我在perl方面没有跟上速度。你能提供它吗?不行。“col”引用应该是xxx。它可以是abc或xyz等。应该保留的唯一逗号是双引号字符串中的逗号。这很好。大多数语法都有文档记录。但是(*SKIP)和(?)不是。你能提供文档参考吗。跟进:这很好。出现了一种新情况,“..A”字符串中有一个换行字符。有没有办法从双引号字符串中删除控制字符?@dansawyer,试试
perl-pe的s/“[^”+”/$&=~s | pattern | replace | gr/ge'ip.txt
。。。这是在所有带引号的字符串中执行的,它正在将
模式
更改为
替换
。。。因此,对于您的情况,
replace
将为空,
pattern
将是您需要删除的任何控制字符