bash脚本阵列为csv [英] Bash script array to csv

查看:117
本文介绍了bash脚本阵列为csv的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从我的阵列做逗号分隔字符串。在我的剧本我收集的数据阵列 outputArr ,然后我想回声它检查出来,但现在我M卡住。我想用打印出来在控制台上:

回声$ {outputArr [*]}

但我发现了错误的输出。所以我想调试它,它这样写:

 回声$ {outputArr [0]}
回声$ {outputArr [1]}
回声$ {outputArr [*]}
回声-------------

确定这里是我的输出:

 此致末期
2204
 2204nally此致
-------------

和其他例如:

 阿尔法
1491
 1491
-------------

这是为什么覆盖与年初的空间?当然,我的目标是(最终):

 阿尔法; 1491年

在这一点:

  Alfa1491

编辑:

 同时读取-r线

    SINGLELINE = $行
    如果[[$ SINGLELINE =〜$ regexVenueName]
    然后
        SINGLELINE = $ {SINGLELINE /<跨度ID = \\ctl00_MainContent_lbPartner \\级= \\合作\\> /}
        SINGLELINE = $ {SINGLELINE /< \\ / SPAN> < BR \\ />< BR \\ /> /}
        partnerVenueNameOutput + =$ SINGLELINE
        outputArr [0] = $ SINGLELINE
    科幻
完成< $ F


解决方案

您数组包含的元素与回车:

  $富=($'\\ rterminally你的$\\ R2204')
$回声$ {foo的[0]}
你终末
$回声$ {foo的[1]}
2204
$回声$ {foo的[*]}
2204inally你

有关您的code,你可以添加以下去除变量回车:

  SINGLELINE = $ {// SINGLELINE $'\\ r'/}

I want to do comma separated string from my array. In my script i collecting data to array outputArr and then i want to echo it to check it out, but now i'm stuck. I want to print it on console using:

echo ${outputArr[*]}

But i'm getting wrong output. So i'm trying to debug it and write it like this:

echo ${outputArr[0]}
echo ${outputArr[1]}
echo ${outputArr[*]}
echo "-------------"

OK here is my output:

Terminally Yours
2204
 2204nally Yours
-------------

and other example:

Alfa
1491
 1491
-------------

Why this is overwritten with space in the beginning? Of course my goal is (finally):

Alfa;1491

In this point:

Alfa1491

EDIT:

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"

解决方案

Your array contains elements with carriage returns:

$ foo=($'\rterminally yours' $'\r2204')
$ echo "${foo[0]}"
terminally yours
$ echo "${foo[1]}"
2204
$ echo "${foo[*]}"
2204inally yours 

For your code, you can add the following to remove the carriage return from the variable:

singleLine=${singleLine//$'\r'/}

这篇关于bash脚本阵列为csv的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆