Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
433 views
in Technique[技术] by (71.8m points)

How to ignore commas within a CSV file being read by bash script

I have some fields that can separate the fields using the comma as delimiter, but some values actually contain commas, such as ""California , CA"" These values are surrounded by quotes to indicate the characters within should be treated as part of the field, but I don't know how to parse it to take this into same values.How to Ignore the comma(,) between string

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use FPAT if your awk supports, its an awks built-in variable to define the regex for fields rather than the delimiters. ITs like a complement to FS which is also awks builtin variable.

Example:

echo 'hey there,ola,"Nice, command",ola' |awk -v FPAT='[^,]+|"[^"]+"' '{print $1}'
hey there
echo 'hey there,ola,"Nice, command",ola' |awk -v FPAT='[^,]+|"[^"]+"' '{print $2}'
ola
echo 'hey there,ola,"Nice, command",ola' |awk -v FPAT='[^,]+|"[^"]+"' '{print $3}'
"Nice, command"
echo 'hey there,ola,"Nice, command",ola' |awk -v FPAT='[^,]+|"[^"]+"' '{print $4}'
ola

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...