-2

My bash script stores data in 2 different variables like -:

Var1="My deployment name is A"
Var2="My deployment url is B"

Note:- Var1 and Var2 contains multiple lines and hence need to export in csv to respective columns i.e. 1st and 2nd column.

I would like to export these 2 values to CSV file so that value of Var1 goes to 1st column and Var2 to 2nd column of same CSV file. like -

enter image description here

Can some one please help me on this ? Thanks in advance.

Ankur Vaish
  • 149
  • 9
  • 1
    Updating your question with additional requirements is dubious. Probably roll back your edit, accept the answer you already received (or post one of your own and accept that if you prefer), and post a new question with your complete requirements if you still need help. (But the answer you got already seems to cover your problem.) – tripleee Jan 11 '21 at 06:51

1 Answers1

2

Two Variables = Two Cells

If the variables never contain special symbols like " or , or linebreaks then it is as simple as

echo "$Var1,$Var2" > file.csv

If there could be a special symbol inside them, you have to quote them, for instance using

printf '"%s","%s"\n' "${Var1//\"/\"\"}" "${Var2//\"/\"\"}"  > file.csv

Two Variables = Two Columns With Multiple Cells (One Cell Per Line)

Without quoting

paste -d, <(echo "$Var1") <(echo "$Var2") > file.csv

With quoting

varToCol() {
  sed 's/"/""/g;s/.*/"&"/' <<< "${!1}"
}
paste -d, <(varToCol Var1) <(varToCol Var2) > file.csv

For a pure bash solution replace the function with the following. Please note: Below bash solution adds an unnecessary empty field at the bottom of the column, if the variable ends with a newline character.

varToCol() {
  local col="${!1//\"/\"\"}"
  printf %s\\n "\"${col//$'\n'/$'"\n"'}\""
}
Socowi
  • 25,550
  • 3
  • 32
  • 54
  • thanks, unfortunately both the options didn't work.I have values like below for Var1 - DMC-wfm10 - [cust01-npr01] - 07.01.00 - 15SEP20 AND Var2 its a URL like - https://us-4.rightscale.com/acct/ – Ankur Vaish Jan 08 '21 at 08:08
  • Also, the Var1 and Var2 contains around 2173 enteries. – Ankur Vaish Jan 08 '21 at 08:15
  • Can you explain *"didn't work"* in detail? I cannot really makes sense of your first comment as the formatting is mangled and therefore ambiguous. Could it be that each variable contains multiple lines and each of these lines should land in its own cell? If so, then please *edit* your question and example so that this important information is included. – Socowi Jan 08 '21 at 09:44
  • Oh sorry for that, yes each variable contains multiple lines and i want that all lines within a variable should print in 1st column and in same for 2nd variable in 2nd column.The first variable contains name of deployment (multiple lines) and 2nd variable contains url's links for respective deployments (again multiple lines). – Ankur Vaish Jan 11 '21 at 03:38
  • Based on [chat communication](https://chat.stackoverflow.com/transcript/message/51340018#51340018) they were [apparently using `sh` instead of Bash](https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash) – tripleee Jan 11 '21 at 06:49
  • Hi, Got success with following command , paste -d, <(printf '%s\n' "$f1") <(printf '%s\n' "$f2") >> bogus.csv , where $f2 is pasted in 2nd column and $f1 is 1st column respectively. The only problem now is $f2 values is getting pasted in 2nd column leaving 1 row as blank and so on .. this one thing as creating problem is overall formatting .. any suggestions as where i am doing things wrong ? – Ankur Vaish Jan 11 '21 at 08:26