Solved

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

Posted on 2007-11-29
7
434 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 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
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Make the most of your online learning experience.
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

631 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