Bash script array to csv

前端 未结 2 1964
无人及你
无人及你 2021-01-21 00:08

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 no

相关标签:
2条回答
  • 2021-01-21 00:56

    To remove any trailing carriage returns from each array element, try

    outputArray=( "${outputArray[@]%$'\r'}" )
    

    However, it would probably be better to examine how outputArray is set in the first place and prevent the carriage returns from being added in the first place. If you are reading from a file that uses DOS line endings, try "cleaning" it first using dos2unix.

    0 讨论(0)
  • 2021-01-21 01:01

    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'/}
    
    0 讨论(0)
提交回复
热议问题