LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   [SHELL] Output formatting (https://www.linuxquestions.org/questions/programming-9/%5Bshell%5D-output-formatting-670912/)

Adrnalnrsh 09-18-2008 06:43 PM

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

Code:

#!/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

chrism01 09-18-2008 09:13 PM

Well, this is pretty horrible, but as no one else has chipped in...

Code:

IFS="
"
for rec in `cat t.t`
do
    echo -n "$rec," >>t1.t
done
sed -i -e 's/[ ]\+/ /g' t1.t
sed -i -e 's/,$//' t1.t
echo >> t1.t

where t.t is your test23.txt

burschik 09-19-2008 01:15 AM

Code:

#!/bin/bash

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


Adrnalnrsh 09-22-2008 10:29 AM

Quote:

Originally Posted by burschik (Post 3285261)
Code:

#!/bin/bash

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


Thanks worked perfect


All times are GMT -5. The time now is 06:56 PM.