Issue
I have a bash
script which stores some values in a variable and outputs each value on a separate line like this:
2288
87
183
30
16
I need the values to be in this format on one line so I can export to CSV
:
2288, 87, 183, 30, 16
I have looked at some examples online of using sed
or awk
but couldn't find an example similar to mine.
Any help would be appreciated.
Solution
paste
is your friend:
$ paste -s -d',' file
2288,87,183,30,16
You can also use tr
or awk
, but you will get a trailing comma:
$ awk '{printf $0","}' file
2288,87,183,30,16,
$ tr -s '\n' ',' <file
2288,87,183,30,16,
Update
Its not a file its a variable I have it doesn't appear to work in the same way?
Kind of:
$ myvar="2288
87
183
30"
$ echo "$myvar" | paste -s -d','
2288,87,183,30
$ echo "$myvar" | tr -s '\n' ', '
2288,87,183,30,
$ echo "$myvar" | awk '{printf $0","}'
2288,87,183,30,
Answered By - fedorqui Answer Checked By - Marie Seifert (WPSolving Admin)