Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 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 Answer

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...