0

[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.

1
  • What do you want to do with the new lines characters, just replace them by spaces or smth? In that case you can pipe echo's output through sed to replace \n by " " Commented Aug 24, 2020 at 12:01

1 Answer 1

1

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"
Sign up to request clarification or add additional context in comments.

4 Comments

thought csv is just a text file with some char as delimiter
The csv "standard" is defined in RFC 4180.
@Socowi thanks for the solution, I executed the script by add the code you shared, am seeing the complete output in linux, but when I copy the file on windows server and open in spreadsheet its showing just first 7 charcter in first column and all other fields are blank
@SreekanthKondaiah I cannot help you with the Windows/spreadsheet problem unless you post the problematic csv file and specify what software you use to open it. Please post these informations by editing your own question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.