Operating System - HP-UX
1833356 Members
3198 Online
110051 Solutions
New Discussion

script output in a nice pattern

 
SOLVED
Go to solution
George Abraham_1
Regular Advisor

script output in a nice pattern

Hi admins,

I have a script that checks my backup status, tapes used and the drives.i have defined four variables
SI=`omnidb -sess -datalist "$datalist" | tail -1 |awk '{print $1}'`
AU=`omnirpt -report session_media -sess $SI | grep A | awk '{print $1}'`
STAT=`omnidb -rpt $SI -detail | grep Status | awk '{print $3}'`
TDU=`omnidb -sess $SI -detail | grep Device | awk '{print $4}' | sort -u`

now i want tp output them in a good coloums or in a html fashion and email it to me.
curently i do a
echo $datalist $SI $AU $STAT $TDU >> /filename
and mail the file to my email. This comes kind of jumbled up

any help will be appreciated...
thanks in advance
George
keep smiling
14 REPLIES 14
John Poff
Honored Contributor

Re: script output in a nice pattern

Hi,

Well, you could use the printf function in 'awk', works pretty good for the quick and dirty formatting, just like the printf in C.

But if you are getting that far with your scripting I would suggest moving to Perl. It can handle all kinds of formatting as well as running the commands for you, grabbing the output, parsing the fields, and printing a nice report and/or emailing it to you in most any format. I've done something similar with the 'omnidb' command in OmniBack here and I have a Perl script that lists all the media used for the last 24 hours to ship offsite.

JP
Sridhar Bhaskarla
Honored Contributor

Re: script output in a nice pattern

Hi George,


use printf instead of echo.

Look at printf's man page for more details on formwatting. The man page contains some good exapmles also.

-Sri
You may be disappointed if you fail, but you are doomed if you don't try
James R. Ferguson
Acclaimed Contributor

Re: script output in a nice pattern

Hi George:

Instead of 'echo', format your output with 'printf'. You can easily columnize your data this way. See the man pages for more information.

Regards!

...JRF...
George Abraham_1
Regular Advisor

Re: script output in a nice pattern


Hi

can you tell me the syntax... Just want to print the 4 variables in a column manner...

printf $datalist $SI $AU $STAT $ADU ?

cheers
George
keep smiling
John Poff
Honored Contributor

Re: script output in a nice pattern

Hi,

You'll have to specify the format of each field. For strings you use '%#s', where # is the size of the field. For decimal integers, use '%#d', and for floating point numbers try '%#.#f'.

I don't know what format your variables are, but if the first one is numeric and the next three are strings you could do something like this:

printf "%8d %10s %15s %8s \n" $SI $AU $STAT $TDU

The '\n' will give you a new line character.

JP
Leif Halvarsson_2
Honored Contributor

Re: script output in a nice pattern

Hi,
Of course printf can be used but this kind of formatting can be done with echo too. Just add some tabs.

echo $datalist"\t"$SI"\t"$AU"\t"$STAT"\t"$TDU
Vincent Stedema
Esteemed Contributor

Re: script output in a nice pattern

Hi George,

A fairly simple approach:

printf "%s | %s | %s | %s | %s" $datalist $SI $AU $STAT $TDU

which would produce output that looks something like:

first | second | third | fourth | fifth

In perl:

perl -wle "print join(' | ',@ARGV)" $datalist $SI $AU $STAT $TDU

Regards,

Vincent
George Abraham_1
Regular Advisor

Re: script output in a nice pattern


hi

I used the tab "\t" saperator and my output looks like this

AUTOSYS_archive 2002/12/18-115 [AU6929] Completed DLT_3_commctl_off
AUTOSYS_online 2002/12/18-114 [AU6929] [AU6944] Completed DLT_3_commctl_off DLT_4_commctl_off
OS_NT_adevweb_offsite 2002/12/19-26 [AV1009] [AV1206] Completed/Failures DLT_1b_commctl_off DLT_2b_commctl_off
OS_NT_asybprod_offsite 2002/12/19-17 [AV1127] [AV1206] Completed DLT_1b_commctl_off DLT_2b_commctl_off
OS_NT_asybtest_offsite 2002/12/19-16 [AV1127] [AV1009] Completed DLT_3b_commctl_off DLT_4b_commctl_off
OS_NT_atrwweb1_offsite 2002/12/19-14 [AV1206] [AV1009] Completed DLT_1b_commctl_off DLT_2b_commctl_off
OS_NT_atrwweb2_offsite 2002/12/19-15 [AV1195] [AV1127] Completed DLT_3b_commctl_off DLT_4b_commctl_off
OS_NT_fmcache_offsite 2002/12/19-3 [AV1195] Completed DLT_2b_commctl_off
OS_NT_fmgenio_offsite 2002/12/19-4 [AV1206] [AV1127] Completed DLT_3b_commctl_off DLT_4b_commctl_off
OS_NT_fmprodapp1_offsite 2002/12/19-5 [AV1206] [AV1195] Completed DLT_1b_commctl_off DLT_2b_commctl_off

any ideas to bring this to proper coloumns are welcome

regards
George
keep smiling
Leif Halvarsson_2
Honored Contributor
Solution

Re: script output in a nice pattern

Hi,
Sorry, there is to big differences in string lenght to use simple tabs. You have to use the printf command and set a suitable column with for each column:

printf "%30s %20s %20s %15s %s" % $datalist $SI $AU $STAT $TDU

You perhaps need to try wit different column with until you get a nice output.
George Abraham_1
Regular Advisor

Re: script output in a nice pattern

hi

Thanks a lot... Any ideas on how to get this to html do as to email in a good looking fashion

regards
George
keep smiling
Sridhar Bhaskarla
Honored Contributor

Re: script output in a nice pattern

Hi George,

See if this simple script can help you. Once the "your_file" is created, run the following script to convert it into html. You need to filling the maximum number of fields (MAXWORDS) your output file will have and the
output file name (SOURCE)and the output file name (HTML). Once you run the script, you will get a html file as you wanted. If it is not helpful, you can use the logic though.

#!/usr/bin/ksh
MAXWORDS=9
HTML=source.html
SOURCE=source

cat << EOF > $HTML






EOF

cat $SOURCE |while read LINE
do

WORDS=0
for ENTRY in $LINE
do
echo "" >> $HTML
(( WORDS = $WORDS + 1 ))
done
if [ $WORDS -lt $MAXWORDS ]
then
while [ $WORDS -le $MAXWORDS ]
do
echo "" >> $HTML
(( WORDS = $WORDS + 1 ))
done
fi
echo "" >> $HTML
done

cat << EOF >> $HTML
Report
$ENTRY






You may be disappointed if you fail, but you are doomed if you don't try
Chris Vail
Honored Contributor

Re: script output in a nice pattern

At the risk of being overly simplistic: I write all my reports as comma-delimited, and email them to myself with a .csv extension. That way, I can open them with a spreadsheet and edit, print or massage the data to my heart's content:

echo "$datalist,$SI,$AU,$STAT,$TDU" >> /filename
uuencode filename filename|mailx -s "Data" myemailaddy@whereIam.com

This attaches the document to the email. From my email box, I just double click the attachment, and it opens up in Excel. This lets me do the formatting and pretty printing without too much shell scripting. Shell scripting is fun, but I'm lazy....

Good Luck
Chris
George Abraham_1
Regular Advisor

Re: script output in a nice pattern

Hi John,

can you post the perl script you told about.. i will appreciate that

thanks
George
keep smiling
John Poff
Honored Contributor

Re: script output in a nice pattern

Hi George,

Here is my Perl script. I posted it as an attachment so that the formatting won't get too hosed up.

The name of the script is used_media.pl. Let me give you a little background on our environment. We use OmniBack and we have two main tape pools. The first pool is for all our non-essential machines [test, development, etc.] and the second pool is for all our critical machines that we would need to recover in case of a disaster. The second pool has the string '-DR' at the end of the pool name, and that is the pool that this script looks for. We use a large LTO tape library, and each day we eject all the tapes used in the -DR pool overnight and send them offsite for three weeks. When we eject the tapes we move them to a pool named Offsite, so that OmniBack doesn't try to use them when they aren't around. Also, we have numbered containers which we have setup as Locations in OmniBack, so when we move tapes to the Offsite pool we also change the location to be the container number, which makes it a little easier to track down and audit tapes.

If you run the script without any options it will give you a report of all the tapes used in the -DR pool for the last 24 hours. Using the -m option will move the tapes listed in the report to the Offsite pool, and using the -c option with a container number changes the location field for each tape. If you specify the -e option the tapes will be ejected to the CAP. We added logic to count the number of tapes being ejected so that the script will pause and wait for input if you have more tapes that can be ejected in the CAP at one time [in this version of the script it is 14 tapes].

This script might do more than you are looking for, but feel free to take it, use it, change it, etc. The report uses the 'format' function in Perl which is a really great way to format a report. If you don't need all the fancy options you can just strip those parts out and use the report.

Have fun!

JP