[SHELL] Output formatting

I am looking for a way to format the output of this string into comma separated values (.csv) so I can import into excel.

Also I want to remove any white space generated by the commands, particularly the dmidecode as the output has lots of space in front of Serial Number.

#!/bin/bash

for i in $( cat "ipaddresses.txt" ); do     # list of ip addresses 
echo "$i" >> host_details.txt;
ssh $i 'hostname ; dmidecode | grep "Serial Number" | head -1 ; free -m | grep Mem | awk '\''{ print $1,$2 }'\'' ; uname -r ; ' >> host_details.txt
done

Current output looks like.

[root@server user]# cat test23.txt
server.domain.com
-WHITE SPACE--Serial Number: F3FF071
Mem: 2007
2.4.21-27.ELsmp

[quote="adrnalnrsh"]
I am looking for a way to format the output of this string into comma separated values (.csv) so I can import into excel.

Also I want to remove any white space generated by the commands, particularly the dmidecode as the output has lots of space in front of Serial Number.

#!/bin/bash

for i in $( cat "ipaddresses.txt" ); do     # list of ip addresses 


There is no need for cat (and it will cause the script to fail if any lines in the file contain spaces).

while read i
do
  : do whatever....
done < ipaddresses.txt

[/INDENT]

What do you want it to look like?

Generally, formatting is done with printf:

## Shell code:
{
  printf "%s," "something"  "something else"   "something new" "something old"  
  echo
} > host_details.txt

Thanks, I am still working on it.

Here is my current code.

#!/bin/sh

HOSTNAME="hostname"
FREE="free -m | grep Mem" 
SSH="ssh"
USER="root"
while read i
do
        $SSH $USER@$i "$HOSTNAME ; $FREE"
done < ipaddresses.txt

{
  printf "%s," "$HOSTNAME"
  echo
} > test_details.txt

The only thing that script will do is print 'hostname,' (without a newline) to test_details.txt

I think what you want is:

HOSTNAME="hostname"
FREE="free -m | grep Mem" 
SSH="ssh"
USER="root"
while read i
do
   $SSH $USER@$i "$HOSTNAME ; $FREE" |
      while IFS= read -r line
      do
        printf "%s," "$HOSTNAME"
      done
      echo
done < ipaddresses.txt > test_details.txt

Or, if you want all the values returned by $FREE to be comma separated (untested):

HOSTNAME="hostname"
FREE="free -m | grep Mem"
SSH="ssh"
USER="root"
while read i
do
   $SSH $USER@$i "$HOSTNAME ; $FREE" |
    {
      read hostname
      printf "%s," "$hostname"
      tr -s " " ,
    }
done < ipaddresses.txt > test_details.txt

Wonderful, thank you. I will give it a try.

Sorry to thread jack, could you explain that a little more please?

Say I have a file with 5 ip address's:
172.16.31.248
172.16.31.249
172.16.31.250
172.16.31.251
172.16.31.252
and I wanted to ping each one of them, I'd typically do something like:

#!/usr/bin/bash
for i in `cat ips.out`;do ping $i;done

What is it that is wrong with this method?
I mean I wouldn't normally put it in a script...but you know what I mean.

In this case, it doesn't matter all that much, but it's an inefficient method. It uses cat (an external command, and therefore slow) unnecessarily, and causes extra work for the shell in splitting the output of cat into separate arguments.

However, if any of the lines contained spaces, it would also split the line and $i would be assigned each word separately.

Right, this much I follow, I guess what I'm asking is, in the example you gave, what is I? I understand why my way is ineffective, but I don't understand the syntax of your "while read..." command. I thought read was for reading from stdin? Maybe I'm misunderstanding the intent of the op's script...

Yes, it is, but stdin can be redirected from a file or a pipe:

while IFS= read -r line
do
  : do something with "$line"
done < /path/to/file