Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2007-11-29
7
Medium Priority
?
448 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1200 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 800 total points
ID: 20380483
awk '{a+=length;print $1 "\t\t" $2;lines++} END {print "Rows--> " lines  "Filesize:--> " a+lines}' myfile.dat
0
Industry Leaders: 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!

 

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 1200 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

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
Starting up a Project
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Suggested Courses

705 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