[HPADM] SUMMARY: Parse file using awk

From: Hazari Shyam (WQQ1SXH) (WQQ1SXH@ups.com)
Date: Wed Sep 17 2003 - 16:45:38 EDT


I received three replies and I ended up using Brett Geer's Script. I added
few more steps to suit to my requirement.
Here is the script.

----------------------begin script-----------------------------------
#!/bin/ksh

awk -F"," ' BEGIN { vTOTAL=0 ; vFILE=0 }
{
   RLENGTH = length($5)
   vNUMBER = substr ($5, 1, RLENGTH-4)
   vTYPE = substr ($5, RLENGTH-2, RLENGTH)
   if (vTYPE == " MB")
      (vNUMBER = vNUMBER / 1024)
   if (vTYPE == " KB")
      (vNUMBER = vNUMBER / 1048576)
   vTOTAL += vNUMBER + 0
   vFILE = substr ($6, index (" ", $6))
   vFILET += vFILE + 0
}

END { print vTOTAL " " vFILET }' < webreport

---------------------------end script-------------------------------------
Also I received replies from David Totch and Ronell. I haven't tested them
yet.

Thank you So much !!!

-Shyam

2)
Try something like this:
BEGIN {
FS="," # set field separator to comma
# use the factors for division (and you thought I was gonna mutiply...)
factor["KB"]=1024*1024
factor["MB"]=1024
factor["GB"]=1
}
{
split($5,array," ") # need the number and dimension separately
printf(">%s< ]%s[ %s\n",array[1],array[2],array[1]/factor[array[2]])
# this just checks conversions, you will need to total.
}
-David T

3)

awk ' BEGIN {
FS = ","
in_file = /in/file/path/name
out_file = /out/file/path/name
read_in = "cat " in_file
While (read_in | getline) {
fs_size_info = $5
### get the number
fs_size = substr(fs_size_info,1,len(fs_file_info)-3)
### get the type
fs_size_type = substr(fs_size_info,len(fs_size_info)-1,len(fs_size_info))
### change to GB
if (fs_size_type != "GB") {
if (fs_size_type == "KB") {
fs_size = fs_size/1024/1024
}
if (fs_size_type == "MB") {
fs_size = fs_size/1024
}
}
fs_size_info = fs_size " GB
### if you want to print the new output to a file
print $1 "," $2 "," $3 "," $4 "," fs_size_info "," $6 >> out_file
### to get the totals
tot_size = tot_size + fs_size
tot_files = tot_files + $6
}
close(read_in)
}'
This is all off the top of my head so you may need to change a few details
but you should get the idea.
-Ronelle van Niekerk

Original Question
Admins,
I got a file in comma separated format and I would like to parse it. I would
like to add the size and no of files column to get a grand total. As you see
some of the contents for size column are in KB,GB and MB. I would like to
convert KB and MB to GB before getting a grand total.

FileSystem Status Date/time Duration Size No
of Files
u001/app/oracle/oradata/iasdb,File Failure,9/15/03 8:14 PM,00:09:28,516.963
MB,30
/u001/app/oracle/product/ias/9.0.2.0.1/inf,File Failure,9/15/03 8:29
PM,01:09:00,4.105 GB,420
/var,File Failure,9/15/03 9:06 PM,00:24:18,1.084 GB,3988
/usr,File Failure,9/15/03 6:26 PM,00:17:27,30.668 MB,27
/u001/app/oracle/product,File Failure,9/15/03 8:40 PM,00:19:20,766.586 MB,71

/data/UPS/prod_flows,File Failure,9/15/03 7:50 PM,00:28:42,1.737 GB,174
/data/UPS/prod_flows,File Failure,9/15/03 7:50 PM,00:28:42,1.737 GB,174
/u001/app/oracle/product,File Failure,9/15/03 9:37 PM,00:02:57,26.604 MB,11
/D/,File Failure,9/15/03 6:32 PM,00:57:01,3.288 GB,181
/C/,File Failure,9/15/03 6:46 PM,00:00:44,135.000 KB,20
/H/,File Failure,9/15/03 9:00 PM,05:23:36,46.717 GB,296690
/H/,File Failure,9/15/03 9:00 PM,01:19:02,1.757 GB,768
/C/,File Failure,9/15/03 9:09 PM,00:00:36,5.463 MB,52
/W/,File Failure,9/15/03 9:17 PM,00:08:17,7.000 KB,10
I am looking for a simple awk snippet.

-Shyam

--
             ---> Please post QUESTIONS and SUMMARIES only!! <---
        To subscribe/unsubscribe to this list, contact majordomo@dutchworks.nl
       Name: hpux-admin@dutchworks.nl     Owner: owner-hpux-admin@dutchworks.nl
 
 Archives:  ftp.dutchworks.nl:/pub/digests/hpux-admin       (FTP, browse only)
            http://www.dutchworks.nl/htbin/hpsysadmin   (Web, browse & search)


This archive was generated by hypermail 2.1.7 : Sat Apr 12 2008 - 11:02:34 EDT