我想从我的数组中做逗号分隔的字符串。在我的脚本中,我将数据收集到数组 outputArr
,然后我想 echo
它来检查它,但现在我被卡住了。我想使用以下命令在控制台上打印它:
echo ${outputArr[*]}
但是我得到了错误的输出。所以我正在尝试调试它并像这样编写它:
echo ${outputArr[0]}
echo ${outputArr[1]}
echo ${outputArr[*]}
echo "-------------"
OK 这是我的输出:
Terminally Yours
2204
2204nally Yours
-------------
和其他例子:
Alfa
1491
1491
-------------
为什么开头会被空格覆盖?当然我的目标是(最终):
Alfa;1491
在这一点上:
Alfa1491
编辑:
while read -r line
do
singleLine=$line
if [[ $singleLine =~ $regexVenueName ]]
then
singleLine=${singleLine/<span id=\"ctl00_MainContent_lbPartner\" class=\"Partner\">/}
singleLine=${singleLine/<\/span> <br \/><br \/>/}
partnerVenueNameOutput+="$singleLine"
outputArr[0]=$singleLine
fi
done < "$f"
最佳答案
您的数组包含带有回车符的元素:
$ foo=($'\rterminally yours' $'\r2204')
$ echo "${foo[0]}"
terminally yours
$ echo "${foo[1]}"
2204
$ echo "${foo[*]}"
2204inally yours
对于您的代码,您可以添加以下内容以从变量中删除回车符:
singleLine=${singleLine//$'\r'/}
关于arrays - Bash 脚本数组到 csv,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21236711/