Solved

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

Posted on 2007-11-29
7
415 Views
Last Modified: 2013-11-13
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
Comment
Question by:gram77
  • 3
  • 3
7 Comments
 

Author Comment

by:gram77
ID: 20380413
I don't want to use wc -l to check for the filesize. I want to awk code.
0
 
LVL 9

Accepted Solution

by:
ghostdog74 earned 300 total points
ID: 20380436


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

Open in new window

0
 
LVL 84

Assisted Solution

by:ozo
ozo earned 200 total points
ID: 20380483
awk '{a+=length;print $1 "\t\t" $2;lines++} END {print "Rows--> " lines  "Filesize:--> " a+lines}' myfile.dat
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 

Author Comment

by:gram77
ID: 20388214
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
 
LVL 9

Expert Comment

by:ghostdog74
ID: 20388326
a+=$2  is equivalent to a=a+$2
0
 

Author Comment

by:gram77
ID: 20388855
ghostdog74:
That i know. Could you please tell me how your solution works?
0
 
LVL 9

Assisted Solution

by:ghostdog74
ghostdog74 earned 300 total points
ID: 20390276
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

Featured Post

Independent Software Vendors: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
list of sudo access for date range 5 34
Centos 7 DNS server not replying to clients 3 51
Java List 4 31
SSSD - Automatic kerberos ticket initialization 1 9
This is an explanation of a simple data model to help parse a JSON feed
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question