Sreekanth Kondaiah
Sreekanth Kondaiah

Reputation: 9

How to insert the multiline commands output into a same cell in csv file while redirecting the ouptut

[enter link description here][1]I am creating a csv file, which contains the server health checks report from the bash script. here am struggling with redirecting multiline command ouptut into a single cell, for me its getting all in different cells.

eg: command is: echo "$server,$RootFreeSpace,$Uptime,$OSVersion,$TotalProcess,$ServerLoad,$Memory,$Disk,$CPU" >> $FILE

$Disk has multiline output,for me its getting redirected in different cells I want to get all the output of any particular command to be redirected into single cell,

Searched a lot but did not found much information.

Upvotes: 0

Views: 506

Answers (1)

Socowi
Socowi

Reputation: 27275

In csv you have to quote linebreaks and other special symbols by writing the whole cell in quotation marks: "cell value". A " inside the cell value has to be escaped by doubling: "".

The following function quotes each each of its arguments as a csv cell and prints all these cells as one csv row.

asCsvRow() {
  printf %s\\0 "$@" |
  sed -z 's/"/""/g;s/.*/"&"/' |
  tr \\0 , | head -c-1
  echo
}

In your case you would use it like

asCsvRow "$server" "$RootFreeSpace" "$Uptime" "$OSVersion" "$TotalProcess" "$ServerLoad" "$Memory" "$Disk" "$CPU" >> "$FILE"

Upvotes: 1

Related Questions