The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > UNIX for Dummies Questions & Answers
.
google unix.com




View Single Post in the UNIX and Linux Forums - Click on the Thread or Permalink to View Entire Thread -->
  #4 (permalink)  
Old 08-23-2008
era era is offline Forum Advisor  
Herder of Useless Cats (On Sabbatical)
  
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,652
Take out the -l from the wc command and you get byte (and word) counts for each file as well. (Why did you add it in the first place?) Some simple post-processing to divide by 1,000 or 1,024 and increasing the unit is easy to add to the awk script. (Do you want MB or MiB, i.e. 1000 or 1024? See http://en.wikipedia.org/wiki/Mebibyte)

Code:
wc /data/XYZ/Daily_File*.txt |
nawk -v OFS=, '{ bytes=$3; suff="KMGT"; i=0;
   while (bytes > 1000) { bytes = int(bytes/1000); i++ }
   print $4, $1, bytes (i > 0 ? (" " substr(suff,i,1) "B") : " bytes") }'
For 1024-byte units, bytes >>= 10 is probably more efficient than bytes = int(bytes/1024)

As an engineering recommendation, I would still suggest that you put the raw numbers in the CSV file, and leave the arrangement of the presentation as KiB or whatever to the consumer of that file.

Last edited by era; 08-23-2008 at 03:12 PM.. Reason: Link to Wikipedia