1833934 Members
1782 Online
110063 Solutions
New Discussion

Help with AWK script

 
SOLVED
Go to solution
Theresa Patrie
Regular Advisor

Help with AWK script

Hi,
I have written a small script to strip out TOTAL REAL-MIN and HOG-FACTOR from the monthly acct file, fiscrptXX. I run into problems when the numbers in some columns are so large that they run together. I have attached a small portion of an acct file with the problem data and also the script I am using which uses AWK to print out the values from column 5 and 8. Can I instruct awk to read column five as a floating point number with 2 decimal points?? Can I re-format the monacct output with wider column widths?? What is the best way to get around this?
Any thoughts are greatly appreciated!
Thanks,
Theresa
This is my easy job!
10 REPLIES 10
Rodney Hills
Honored Contributor

Re: Help with AWK script

Use "substr" of awk.

awk 'print substr($0,39,13),substr($0,68,12)'

to print those column positions.

HTH

-- Rod Hills
There be dragons...
James R. Ferguson
Acclaimed Contributor

Re: Help with AWK script

Hi Theresa:

You can always use the shell to reformat the overlapping columns of your file:

#!/usr/bin/sh
while read X
do
A=`echo "${X}"|cut -c1-51`
B=`echo "${X}"|cut -c52-`
echo "${A} ${B}"
done < infile > outfile

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: Help with AWK script

If you are to do more with the data, consider using perl's pack:

#!/use/bin/perl

use strict;
use warnings;

my $f1 = "/var/adm/acct/fiscal/fiscrpt".((localtime)[4]+1);
my $f2 = $f1 . "_numbers";

open my $F, "< $f1" or die "$f1: $!";
while (<$F>) {
my ($cmd, $n, $tot, $cpu, $real, $mean, $min, $hog, $xfer, $read) =
grep s/^\s*//,
unpack "A9 A7 A12 A10 A13 A8 A8 A8 A13 A11", $_;
$real =~ m/^\d/ or next;
printf "%13s %8s\n", $real, $min;
}
Enjoy, Have FUN! H.Merijn
Theresa Patrie
Regular Advisor

Re: Help with AWK script

Rod & James,
Thanks for the ideas, but unfortunately once the line is read into $X, it loses all of its nice formatting. I've attached what $X looks like after the "read". Unless character position can be used on the "read", it is virtually useless after the fact. The only way I can think to break up the numbers that run together is to read up to only 2 places after a decimal point, but I do not know if this is possible.
Procura, I do not know anything about Perl, but would that script take care of the issues I've outlined in my response?? How does that script deal with the numbers that have run together?
Any other ideas??
Thanks Again,
Theresa
This is my easy job!
James R. Ferguson
Acclaimed Contributor
Solution

Re: Help with AWK script

Hi (again) Teresa:

Please run the small script I suggested. By *double-quoting* the variables in the 'echo' statements, I have preserved the formatting you had in the input file while adding one blank character to separate the merged fields.

Regards!

...JRF...
H.Merijn Brand (procura
Honored Contributor

Re: Help with AWK script

The unpack splits the line into the fields as declared with the 'my' line, stripping *trailing* spaces for each field. If you want them kept, use a9 and such (small a a.o.t. capital A):

my ($cmd, $n, $tot, $cpu, $real, $mean, $min, $hog, $xfer, $read) =
unpack "A9 A7 A12 A10 A13 A8 A8 A8 A13 A11", $_;

The grep I had in between stripped off the *leading* space for every field. I did this to be able to filter on the wanted columns starting with a digit. If I leave the grep out, I have to change the next line to

$real =~ m/^\s*\d/ or next;

Enjoy, have FUN! H.Merijn (who still wants an option in the ITRC to paste formatted text and prevent clipping the multiple spaces from nicely formatted tables, so I can finally show how well it works)
Enjoy, Have FUN! H.Merijn
Theresa Patrie
Regular Advisor

Re: Help with AWK script

Hi James,
Sorry I did not look more closely at your response the first time. It does exactly what I need. I have a few other special cases to handle, but I think I can take care of those. You wear your ITRC Olympian status well!!

Thanks for the explanation, Procura. Looks like a foreign language to me since I do not know Perl, but I may check into it more closely if I have more time. Thanks for your input!
As usual, ITRC comes to the rescue!
Thanks,
Theresa
This is my easy job!
Rodney Hills
Honored Contributor

Re: Help with AWK script

If you do go the "perl" route, then I recommend reading the acct records directly.

Check out
http://www.perl.com/pub/r/916

This describes a module you can use within perl to process the "acct" info. This way you can munge the data how ever you like and not worry about number field "overflows"

-- Rod Hills
There be dragons...
Theresa Patrie
Regular Advisor

Re: Help with AWK script

Thanks for the info Rod, but I am talking about HP's built in process accounting. Looks like that Perl routine is for user accounting (passwords etc.)
Thanks anyway!
This is my easy job!
Gary Yu
Super Advisor

Re: Help with AWK script

Hi Theresa ,

to direct awk to read/print a column as floating decimal, you can use the format like in C, in your case, you can add format like this:

awk '{print "%.2f\n", $5}'

I tested it with your sample data, and got the following output of column 5:
1060.77
144331.87
181.58
73304.79
3542544.38
1421.01
194178.39
27.79
17.78
0.00


thanks,
Gary