Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 396
  • Last Modified:

file sizing

I need tro perform several unix command in AIX to obtain information on files,
so how would you do the following:

-  Count the number of rows in a file?
-  Get the length of the largest row in the file
-  Then multiply these two to get an exstimated file size

Thanks
0
mmatharu
Asked:
mmatharu
  • 6
  • 6
  • 4
  • +1
1 Solution
 
sunnycoderCommented:
Hi mmatharu,

you can get the exact size by using stat or ls -l command

stat <filename>
will show you the exact filesize.

Cheers!
Sunnycoder
0
 
sunnycoderCommented:
Hi mmatharu,

If you want stat to produce only the filesize
stat -t CallingFeatures.h | awk ' { print $2}

Alternatively you can use
du <Filename>

-H option would show it in human readable format (kB, MB etc instead of just numbers)

Cheers!
Sunnycoder
0
 
XoFCommented:
print number of lines: wc -l
print length of longest line: wc -L

calculate est. file size:

wc -l -L <filename> | awk '{print "Num. of lines:\t"$1"\nLongest line:\t"$2"\nEst. Filesize:\t"$1*$2"\n"}'

HTH,
-XoF-
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
XoFCommented:
improved to also print real filesize:

wc -l -c -L <filename> | awk '{print "Num. of lines:\t"$1"\nLongest line:\t"$3"\nEst. Filesize:\t"$1*$3"\nReal Filesize:\t"$2"\n"}'

Regards,

-XoF-
0
 
mmatharuAuthor Commented:
i am trying this

print length of longest line: wc -L

it is returning wc Not a recognized flag: L

0
 
sunnycoderCommented:
Is there any particular reason why you wish to take the long winded way when there is a more accurate and simple method available?
0
 
mmatharuAuthor Commented:
also I am using AIX
0
 
ahoffmannCommented:
> -  Count the number of rows in a file?
what is a row?
If you mean that a row is anything left of \r and/or \n then use
wc file

> -  Get the length of the largest row in the file
assuming rows to be terminated by \r and/or \n
awk '{l=length()}END{print l}' file

> -  Then multiply these two to get an exstimated file size
why would you do that if ls -l gives an 100% exact size of the file?
0
 
XoFCommented:
> why would you do that if ls -l gives an 100% exact size of the file?

I'd guess homework...;)

> awk '{l=length()}END{print l}' file

Hmm, isn't "l" overwritten with each iteration (each line)? I'd have assumed something like
awk '{l=length();if(max < l){max=l}}END{print max}' file

Regards,

-XoF-
0
 
sunnycoderCommented:
>I'd guess homework...;)

Then we shouldn't be giving away a coded solution
0
 
XoFCommented:
So the complete solution would look like that:

awk '{l=length();if(max < l){max=l};i++}END{print "Num. of lines:\t"i"\nLongest line:\t"l"\nEst. Filesize:\t"i*l"\n"}' file

regards,
-XoF-
0
 
XoFCommented:
> Then we shouldn't be giving away a coded solution

well, that's such an easy one. Hardly possible to just give hints without providing a working solution. Nevertheless, you're correct. A simple "man wc" or a link to an awk tutorial would have been the better approach. But that hadn't made me thinking about that stuff...

regards,

-XoF-
0
 
ahoffmannCommented:
oops, my awk was too lazy, but it works if the last line is the longest :-))
Thanks for correcting XoF.

>I'd guess homework...;)
agreed, I guess that such simple thinks won't be asked at any school, anyway the general answer then is
man ls
man wc
man awk
0
 
XoFCommented:
> ... but it works if the last line is the longest :-))

YMMD! ROFL.  :-))

0
 
mmatharuAuthor Commented:
Firstly,

Its not homework, I'm new to unix and aix and need to get an estimation on filesizes in order to calulate disk space.

Thank you for you help.
0
 
sunnycoderCommented:
>need to get an estimation on filesizes in order to calulate disk space.

du -H
df -H
0
 
mmatharuAuthor Commented:
-H it is not a recogised command is AIX
0
 
sunnycoderCommented:
man du
man df
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 6
  • 6
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now