我不认为有一种方法可以让这个命令忽略引号内的逗号(在'三'之间)?
$ echo 'one,two,"three,three",four' | { IFS=',' read -a array; echo ${#array[@]}; }
5
的Nb。 echo ${#array[@]}
报告数组长度..
答案 0 :(得分:2)
在gnu-awk中使用FPAT
,你可以这样做:
str='one,two,"three,three",four'
awk -v FPAT='"[^"]*"|[^,]*' '{
print "# of columns: " NF; for(i=1; i<=NF; i++) print "Field #", i, ":", $i
}' <<< "$str"
<强>输出:强>
# of columns: 4
Field # 1 : one
Field # 2 : two
Field # 3 : "three,three"
Field # 4 : four
答案 1 :(得分:1)
转义逗号:
echo 'one,two,"three\,three",four'
结果:
$ echo 'one,two,"three\,three",four' | { IFS=',' read -a array; echo ${#array[@]} ${array[2]}; }
4 "three,three"