我需要分析在QUESTION NO:和第一个答案A.之间出现的文本,而不包括包含QUESTION NO:或A.的行。因此,只显示问题。我想将每个匹配的结果/问题附加到BASH中的数组中,这样每个问题都可以使用类似(针对问题16(的东西来检索:echo ${questions[16]}.
我的test.txt看起来像这样,例如:
QUESTION NO: 16
Which of the following is the correct answer
based on a supplied criteria for the values in the records?
A. LIMIT
B.....
C..
命令:
sed -n '/NO:/,/A./{//!p}' test.txt > justquestions.txt
似乎生成了正确的输出,但是我无法使用sed命令将每个结果附加到内存中的数组中。我一直在获取整个输出,这样每个问题就不会被一个数组号跟踪。
如何将每个匹配追加到sed/或类似命令输出的类似questions+=( "${p}" )
的数组中?有时问题是一行,有时多行含特殊字符。谢谢!
如果多选答案:A.B.C.D.E.不存在,由于问题是填空,数组中会附加一行不正确的内容。
例如文本:
QUESTION NO: 123
What is the best command ever (fill in the blank ____)?
Answer: reboot
echo${Q_answer[4]}返回:
What is the best command ever (fill in the blank ____)? Answer: reboot
而不是预期的:
What is the best command ever (fill in the blank ____)?
也许"end_re="也可以在出现"Answer:"或任何其他文本时停止,而不是"A."。我想知道在"?"处停止是否是最好的主意。。。
start_re='^[[:space:]]*QUESTION NO:[[:space:]]+([[:digit:]]+)$'
end_re='^[[:space:]]*(A[.]|Answer:)'
curr_q= # track current question number
questions=( ) # initialize array
while read -r line; do
if [[ $line =~ $start_re ]]; then # if we see the start of a question
curr_q=${BASH_REMATCH[1]} # set the current question number
continue # and don't process this line further
fi
if [[ $line =~ $end_re ]]; then # if we see the end of a question
curr_q= # clear the current question number
continue # and don't process this line further
fi
if [[ $curr_q ]]; then # when a question number exists
if [[ ${questions[$curr_q]} ]]; then # and we already have text for it
questions[$curr_q]+=$'n'"$line" # append additional text after a newline
else # otherwise, if we have no existing content
questions[$curr_q]=$line # assign the line we just read outright
fi
fi
done
在以给定的问题作为输入运行以上操作后,declare -p questions
会发出以下状态:
declare -a questions='([16]="Which of the following is the correct answer
based on a supplied criteria for the values in the records?")'