csv file in bash

Posted on 2009-04-23
Last Modified: 2013-12-26
I'm running a loop using EOF to log into each server in my file serverlist and then run the commands but I want the output to be in csv format in order that I can open it in Excel. I need to be able to remove the new line chars in order that it's csv format but I tried to append  | tr "\n" to the commands but it didn't help. I have another question open where I tried to do is slightly different to here but that didn't work either

for server in `cat serverlist`; do
ssh -q $server <<-EOF 2>&1 >>output.log
sudo su -
hostname | awk -F'.' '{print $1}'; echo "," cat /proc/meminfo | grep MemTotal; echo "," cat /proc/cpuinfo | grep "model name" | head -1 | awk -F':' '{print $2}' ; echo "," cat /proc/cpuinfo | grep -c processor

echo "$info1,$info2,$info3,$info4"

sed -e /^Logged/D -e /^WARNING/D -e /^"Initializing profile"/D -e /^$/D output.log>excel.csv
Question by:jaysilverheels
    LVL 20

    Expert Comment

    Looks like you had already thought of a way, assigning the output of each command to a variable and then outputting the variables?

    info1=`hostname | awk -F'.' '{print $1}'`
    info2=`cat /proc/meminfo | grep MemTotal`
    info3=`cat /proc/cpuinfo | grep "model name" | head -1 | awk -F':' '{print $2}'
    info4=`cat /proc/cpuinfo | grep -c processor`

    echo "$info1,$info2,$info3,$info4"

    LVL 29

    Expert Comment

    Try this:

    # ...Etc...
    hostname | awk -F'.' '{printf "%s,",$1}'; cat /proc/meminfo | grep MemTotal | tr '\n' ',' ; cat /proc/cpuinfo | grep "model name" | head -1 | awk -F':' '{printf "%s,",$2}'; cat /proc/cpuinfo | grep -c processor
    # ...Etc...

    Open in new window

    LVL 68

    Accepted Solution

    Please look at your other Q - 24346213
    It should work as I suggested there.

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap ( Version 1.2 2.      Jpcap( Version 0.6 Prerequisite: 1.      GCC …
    Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
    This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
    In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now