• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

How do I calculate the filesize of myfile.dat in awk?

Here is the script
cat myfile.dat | awk '{print $1 "\t\t" $2;lines++} END {print "Rows--> " lines  "Filesize:--> "}'

Data/Result:
TRX               TRXAMT
10              100
20              200
30              300
40              400
50              500
Rows-->5 Filesize-->
0
gram77
Asked:
gram77
  • 3
  • 3
3 Solutions
 
gram77Author Commented:
I don't want to use wc -l to check for the filesize. I want to awk code.
0
 
ghostdog74Commented:


awk 'NR>1{a+=$2;r++}END{print "Rows=>" r " Filesize=>" a}' myfile.dat

Open in new window

0
 
ozoCommented:
awk '{a+=length;print $1 "\t\t" $2;lines++} END {print "Rows--> " lines  "Filesize:--> " a+lines}' myfile.dat
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
gram77Author Commented:
ghostdog, ozo:
can you explain how your program works.

Ghostdog: how does a+=$2 calculate file size
ozo: how does a+lines calculate file size.
0
 
ghostdog74Commented:
a+=$2  is equivalent to a=a+$2
0
 
gram77Author Commented:
ghostdog74:
That i know. Could you please tell me how your solution works?
0
 
ghostdog74Commented:
awk 'NR>1{a+=$2;r++}
END {
     print "Rows=>" r " Filesize=>" a
}' myfile.dat

NR>1 skips the first record. For every other records, perform a=a+$2. This means $2 (which is your filesizes) is added to 'a' and keeps accumulating the results. 'r' is incremented, just so that later we can use the final value of 'r' as the total number of rows. Actually, it sould be the same as NR-1 (since we skipped the first row.
the END portion just prints the final result format.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now