[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 643
  • Last Modified:

csv file in bash

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
1 Solution
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"

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

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

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now