Utilities to Tidy Up Your Reports


Utilities to Tidy Up Your Reports

 

October 2015 | by David Tansley

 

 

We system administrators quite often have to generate reports either for ourselves or for management. When we generate reports, particularly ad-hoc ones, the output can be quite messy. Admins can usually overlook it but management often needs more—particularly regarding the column formats.

To help you tiding up a bit, let’s look at some utilities. The first utility to come to my mind is AWK but AIX offer others utilities that will do the job. For this article, let’s concentrate on AWK (printf), tab, expand and cut.

I’ll use the file Listing 1. test.txt to demonstrate the built-in text processing utilities. The output is a bit of a mess (imagine if the file contained many entries; not too readable) so while it’s small, it will suffix for these purposes. The columns are hostname, userid, login, auth, shell:

$ cat test.txt
uk0110   goop43          false    false bsh    
uk0111   secpriv1        true     true  sh 
uk0111   dxtans          false    true  sh   

printf

printf is the most popular way to format text from a file by far. It’s generally used with AWK but the SHELL has its own version of printf (though not as flexible as AWK). The script follows:

#!/bin/sh
cat test.txt| while read c1 c2 c3 c4 c5
do
printf "%-8s %-10s %-6s %-6s %-4s\n" $c1 $c2 $c3 $c4 $c5
done

uk0110   goop43      false     false  bsh 
uk0111   secpriv1    true      true   sh  
uk0111   dxtans      false     true   sh

Here we read in the five columns of text (c1-c5) and use printf to print it out. This printf statement states to left justify the first columns text with eight character width and treat the column as a string (%-8s), and so on for all the columns printed. Note though the rest of the columns I have specified use different widths because it look cleaner. You can experiment with the column widths to suit your needs.

Sometimes it’s better to have a header at the top of the output. This is easy enough—just put it before the whole loop is executed. In this example, it would be placed before the cat command in the script. If not, expect repeating headers when the script is executed:

printf "%-8s %-10s %-6s %-6s %-4s\n" HOST ID LOGIN AUTH SHELL

HOST     ID         LOGIN      AUTH   SHELL
uk0110   goop43      false     false  bsh 
uk0111   secpriv1    true      true   sh  
uk0111   dxtans      false     true   sh

For completeness, here is AWK at work. In this example, we stay right justified on the columns, but we are displaying them with greater character spaces, starting with 25 characters:

 $ cat test.txt | awk '{printf "%25s %10s %10s %10s %10s\n",$1,$2,$3,$4,$5}'
           

                    uk0110  goop4      false      false     bsh
                    uk011   secpriv1   true       true      sh
                    uk011   dxtans     false      true      sh

This output more or less centers the text on the screen.

There can be occasions when you have produced the report and handed it over but it comes back to you with an additional request. For example, “Can you give me host uk0111 and what SHELL secpriv1 has.” This is easy as well. AWK is your friend for further manipulation of the text.

In the following, write a conditional expression to state column1 is equal to ‘uk011’ AND column 2 is equal to ‘secpriv1’. Then output columns 1 and 3. No need for print formatting here; it would be overkill.

$ cat test.txt | awk '($1=="uk011") && ($2=="secpriv1") {print $1," ",$5}'
uk011   sh

You may get a different request to perhaps exclude entries. AWK again does it nicely, but you can use SED as well. I like AWK. The following commands excludes all entries that have uk0110, by using the NOT operator:

$ cat test.txt |awk '!/uk0110/ {printf "%25s %10s %10s %10s %10s\n",$1,$2,$3,$4,$5}'
                    uk011   secpriv1       true       true         sh
                    uk011     dxtans      false       true         sh

Tab, Cut and Expand

There may be a requirement to produce a report that will be imported into Excel. Tabs are fine as a delimiter, but I always find it best to provide a ASCII character as a delimiter, such as a comma, which is pretty much standard. We can achieve this with tr utility to replace the tabs with a space, then using cut we simply insert a comma to separate the fields, like so:

$ cat test.txt | tr '\t' ','|cut  -d ' ' -f1,2,3,4,5
uk0110,goop4,false,false,bsh
uk011,secpriv1,true,true,sh 
uk011,dxtans,false,true,sh   

To quickly expand a text column output into a more readable format, use expand, which converts spaces to tabs. Here we specify with the ‘t’ option how far you want the tab stops to be. In this example, it’s 10 spaces:

$ expand -t10 test.txt
uk0110    goop4     false     false     bsh
uk011     secpriv1  true      true      sh 
uk011     dxtans    false     true      sh

If your text is separated by spaces, then use the tab command. Either way AIX caters for your needs.

A Final Tip

Quite often, I’m asked to number the lines in a resulting report output. There are many ways to do this, but my favourite quick-fire method is using expand and nl. The option ‘ba’ states number all lines of text in the file. Pretty neat.

$ expand -t10 test.txt| nl -ba
     1  uk0110    goop4     false     false     bsh
     2  uk011     secpriv1  true      true      sh 
     3  uk011     dxtans    false     true      sh  

I hope these tips will help you quickly and easily make reports more readable for your audience.



Article Number: 92
Posted: Wed, May 30, 2018 11:08 AM
Last Updated: Wed, May 30, 2018 11:08 AM

Online URL: http://kb.ictbanking.net/article.php?id=92