我的文件看起来像
"dog" 23 "a description of the dog" 123 456 "21"
"cat" 5 "a description of the cat" 987 654 "22"
我正在将文件逐行加载到数组中
filename=$1
while read -r line
do
animal_array=($line)
*do stuff
done < $filename
我想看的东西
animal_array[1] --> "dog"
animal_array[2] --> 23
animal_array[3] --> "a description of the dog"
animal_array[4] --> 123
animal_array[5] --> 456
aninal_array[6] --> "21"
我得到的是
animal_array[1] --> "dog"
animal_array[2] --> 23
animal_array[3] --> "a
animal_array[4] --> description
animal_array[5] --> of
animal_array[6] --> the
animal_array[7] --> dog"
animal_array[8] --> 123
animal_array[9] --> "21"
在将行读入数组之前,努力寻找一种检查“引号”的方法。引号必须在数组中。
答案 0 :(得分:0)
也就是说,如果您只需要处理带有文字数据的双引号字符串(没有转义FPAT
,则GNU awk扩展\"
可以用于您在此处请求的那种解析)内的引号或其他奇数):
split_quoted_strings() {
gawk '
BEGIN {
FPAT = "([^[:space:]\"]+)|(\"[^\"]+\")"
}
{
printf("%d\0", NF)
for (i = 1; i <= NF; i++) {
printf("%s\0", $i)
}
}
' "$@"
}
# replace this with whatever you want to have called after a line has been read
handle_array() {
echo "Read array with contents:"
printf ' - %s\n' "$@"
echo
}
while IFS= read -r -d '' num_fields; do
array=( )
for ((i=0; i<num_fields; i++)); do
IFS= read -r -d '' piece
array+=( "$piece" )
done
handle_array "${array[@]}"
done < <(split_quoted_strings)
...正确发出作为输出:
Read array with contents:
- "dog"
- 23
- "a description of the dog"
- 123
- 456
- "21"
Read array with contents:
- "cat"
- 5
- "a description of the cat"
- 987
- 654
- "22"