[SHELL] Output formatting


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SHELL] Output formatting
# 1  
Old 09-18-2008
[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
# 2  
Old 09-18-2008
Quote:
Originally Posted by 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.

[CODE]
#!/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).
Code:
while read i
do
  : do whatever....
done < ipaddresses.txt

Quote:
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


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

What do you want it to look like?

Generally, formatting is done with printf:

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

# 3  
Old 09-19-2008
Thanks, I am still working on it.

Here is my current code.

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

# 4  
Old 09-19-2008

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

I think what you want is:

Code:
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):

Code:
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

# 5  
Old 09-19-2008
Wonderful, thank you. I will give it a try.
# 6  
Old 09-20-2008
Quote:
Originally Posted by cfajohnson
[INDENT]
There is no need for cat (and it will cause the script to fail if any lines in the file contain spaces).
Code:
while read i
do
  : do whatever....
done < ipaddresses.txt

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:

Code:
#!/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.
# 7  
Old 09-20-2008
Quote:
Originally Posted by DeCoTwc
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:

Code:
#!/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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

SQL Query in Shell Script output formatting

Hi All, #!/bin/ksh call_sql () { sql=$1 sqlplus -s $sqlparam_sieb <<EOF SET ECHO OFF; SET NEWPAGE NONE; SET SQLBL OFF; SET VERIFY OFF; SET LINESIZE 2000; SET... (2 Replies)
Discussion started by: Kevin Tivoli
2 Replies

2. Shell Programming and Scripting

Formatting output

I have the output like below: DEV#: 9 DEVICE NAME: hdisk9 TYPE: 1750500 ALGORITHM: Load Balance SERIAL: 68173531021 ========================================================================== Path# Adapter/Path Name State Mode Select Errors 0 ... (4 Replies)
Discussion started by: Daniel Gate
4 Replies

3. AIX

Help Formatting Output

I am using FORTRAN 90 on AIX 5.3 and need to output my data to a tab-delimited file. It must have actual tabs, and I cannot figure out a way to make it work. The resulting file will be imported into another application (quickbooks) as an .iif file....for some reason, it needs the tabs; spaces do... (2 Replies)
Discussion started by: KathyB148
2 Replies

4. Shell Programming and Scripting

Formatting Shell script output to Excel

I am facing a problem formatting the output of my shell script in excel. We are directing the output of the script to an excel sheet and need long integer type data printed in Excel as it is (i.e. not in the default scientific notation). Also, leading zeroes(if any) in the output are getting... (4 Replies)
Discussion started by: bornali.p
4 Replies

5. Shell Programming and Scripting

Formatting the output

Hi, I have a file which contents entries in this form. Only in /data4/temp abc.000001 Only in /data4/temp abc.000003 Only in /data4/temp abc.000012 Only in /data4/temp abc.000120 Only in /data4/temp abc.000133 Only in /data4/temp abc.001444 i want to read line by line and format... (2 Replies)
Discussion started by: arijitsaha
2 Replies

6. Shell Programming and Scripting

Problem in formatting output of SQL query in excel sheet in shell script

Hi Guys.. Need your help to format the output of my shell script. I am using spool command to take out put in csv file. below is my code. (for example) col USERNAME for a15 col EMAIL for a30 col FULL_NAME for a20 col LAST_LOGIN for a40 col DATE_CREATED for a40 SPOOL 120.csv... (3 Replies)
Discussion started by: Agupte
3 Replies

7. Shell Programming and Scripting

sql select command output formatting in shell script

Hi, I need to connect to the database and retrieve two variables from the database and store them in a variable,out of these two variables I need to get lastdigit appended to the variable 1 retrieved and variable 2 with out any modification in short select var,data from usage; o/p=... (1 Reply)
Discussion started by: rkrish
1 Replies

8. Shell Programming and Scripting

Formatting the query output using shell script

Hi All, I'm finding it difficult to format the query output that was returned in a shell script. Actually i have one shell script which does some DB stuff and depending on the result it will do some more tasks. My question here is to format the query output returned by mysql. Intitally my... (5 Replies)
Discussion started by: RSC1985
5 Replies

9. Shell Programming and Scripting

Output Formatting

Hi Guys I need help removing some lines from output i am receiving from a shell script. Here is the output: http://i52.tinypic.com/10z0fut.png I am trying to remove the output that i have circled. . ${EDW}/extracts/bin/extracts_setup2.sh . ${EDW}/extracts/extracts.conf ... (7 Replies)
Discussion started by: mooey1232003
7 Replies

10. Shell Programming and Scripting

formatting output

Hi need some advice.. #grep -i hostname test.csv (gives the below output) HOSTNAME,name,host_test,,,,,,,, Now I need to format the above output as below. HOSTNAME: name=host_test Any easy way of doing this using awk or sed or printf? (4 Replies)
Discussion started by: balaji_prk
4 Replies
Login or Register to Ask a Question