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: 460
  • 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Featured Post

Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

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