Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to get file size 2

Status
Not open for further replies.

ediforu

IS-IT--Management
Oct 4, 2002
8
US
how can i get the filesize of a file from inside the
awk program?

Thanks in advance
edi
 
hint: any OS commands are accessible from within awk with "system" call. vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
yes, i know
but how do i get that value into a variable back in the
awk program?
 
&quot;date&quot; | getline curDate; close(&quot;date&quot;); vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
requires gawk 3.1+

function dirlist(dir,pat, array1,x) {
if (!pat || !dir) {
perror(&quot;No pattern or directory for file matching.&quot;, 1)
}
command = &quot;ls -l &quot; dir pat &quot; | sort&quot;
while ((command |& getline) > 0) {
array1[$9] = $5 / 1024
}
parray(array1,x)
tlist = array_to_list(array1)
return tlist
}

function array_to_list(arrname, tlist,m) {
for (m in arrname) {
tlist = length(tlist) < 1 ? arrname[m] : tlist&quot; &quot;arrname[m]
}
return tlist
}

function perror(msg,flg) {
printf &quot;%s\n&quot;, msg

if (flg) {
exit
}
return
}


function parray(arrname,len, x) {
while (x <= len) {
print arrname[x++]
}
return
}

BEGIN {
x = 0;
xlist = dirlist(&quot;/home/me/&quot;,&quot;*.txt&quot;)
if (xlist) {
split(xlist,arr)
for (x in arr) {
print x , arr[x]
}
}
}
{
more stuff
}


It's a lot easier in the shell.
 
Disregard the parray line, artifact of an earlier script
using these functions, sorry.
 
thanks vlad, that answers part of my question
could be so kind to take a look at this:
I am parsing a log file

Oct 2 05:50:07 /mniprod/xmit/o.coneg.1002020346
Oct 2 05:50:15 /mniprod/xmit/o.coneg.1002020347
Oct 2 05:50:27 /mniprod/xmit/o.coneg.1002020356
Oct 2 05:51:08 /tsiprod/xmit/o.tetra.1002020422

I need to get the file size of the file in the log.

thanks in advance
 
Got it !
thanks vlad marsd
you are cool
 
something like that to start you off - for Solaris:
[no error checking]

nawk -f getSize.awk getSize.txt

#--------------- getSize.txt
Oct 2 05:50:07 /bin/ls
Oct 2 05:50:15 /bin/nawk
Oct 2 05:50:27 /bin/tar
Oct 2 05:51:08 /bin/cut

#--------------- getSize.awk
BEGIN {
lsSIZE=5
filePath=4
CMD=&quot;ls -l&quot;
}

{
cmd=CMD &quot; &quot; $filePath
cmd | getline fileSpec; close(cmd);
split(fileSpec, fileSpecARR, &quot; &quot;);
filesARR[$filePath]=fileSpecARR[lsSIZE];

}

END {
for (i in filesARR)
printf(&quot;%s -> [%s]\n&quot;, i, filesARR);
}
vlad
+---------------------------+
|#include<disclaimer.h> |
+---------------------------+
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top