我正在使用逗号作为分隔符从csv文件中剪切字符串。有没有办法在分隔符中添加一个选项来忽略包含分隔符的某些字符串?
我想忽略所有用引号写的逗号。
示例:
asdf,1337,"asdf, asdf"
应该成为
asdf
1337
asdf, asdf
而不是
asdf
1337
"asdf,
asdf"
答案 0 :(得分:1)
这个awk代码应该有所帮助:
awk '{$0=$0","; while($0) { match($0,/ *"[^"]*" *,|[^,]*,/);
field=substr($0,RSTART,RLENGTH);
gsub(/^ *"?|"? *,$/,"",field);
print field; $0=substr($0,RLENGTH+1) }}' file
以你的例子:
kent$ echo 'asdf,1337,"asdf, asdf"'|awk '{$0=$0",";
while($0) {
match($0,/ *"[^"]*" *,|[^,]*,/)
field=substr($0,RSTART,RLENGTH)
gsub(/^ *"?|"? *,$/,"",field)
print field
$0=substr($0,RLENGTH+1)
}}'
asdf
1337
asdf, asdf