letoh’s workshop

August 31, 2008

統計檔案大小

Filed under: Note, perl — letoh @ 5:01 pm

隨手寫了一個 script,用來統計檔案大小

#!/usr/bin/perl -p
BEGIN { my $total = 0; }
/(\d+)/; $total += $1;
END { print “Total: $total\t” . ($total / 1024) . “M\n”; }

存成 ts 後可以搭配 duls -s 使用,或者是輸出為每一行開頭有數字的任何指令,例如 seqnl

$ seq 5 | ts
$ ls / | nl | ts

當然這只是好玩而已,沒什麼實際用途

突然想到這幾年陸續接觸了幾種不同的程式語言,包括平日使用最多的 python。但日常使用其實很少出現複雜的應用,大多數情況用 perl 處理起來還是簡潔多了 (雖然這個 perl script 寫得有點像 awk script…)

最後附一下 awk 版:

BEGIN { total = 0 }
total += $1
END { print “Total:”, total,”\t”,total/1024,”M” }

No Comments Yet »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.