- 论坛徽章:
- 145
|
回复 1# Lovcplus
Key point: CNT[$0]++
$ awk -vBAR_DEBUG_TEN=1 -f bar.awk 500M.txt
Read 500M.txt ... (for lines only by wc utility)
500M.txt has 18213494 lines
11:51:32 start, total items:18213494
10.0% [======|.....................................................] 00:05
20.0% [============\...............................................] 00:10
30.0% [==================*.........................................] 00:15
40.0% [========================*...................................] 00:28 took more time
50.0% [==============================/.............................] 00:45
60.0% [====================================|.......................] 01:21
60.8% [====================================\.......................] 01:32^C
$ cat bar.awk
function bar_set(num, msg, cmd, wid, len,dot){
_bar_sec_sys = systime();
printf("%s start, total items:%s %s\n", strftime("%H:%M:%S"), num, msg);
_bar_per_all = num;
_bar_wid = 80;
_bar_stp = int(num /20000)+1;
cmd = "tput cols";
cmd | getline wid;
close(cmd);
if(wid != 0) _bar_wid = wid;
#123456789012345678901234567890123456789012345678901234567890
#100.0% [====================] 01:00:00
# (6) 7 (>20) 30 (8) 39
if(_bar_wid < 40){
print "The screen columns is too small(" _bar_wid ")";
print "Please change it and more than 39" ;
exit(1);
}
_bar_dot_all = _bar_wid - 20;
_alv[1] = "*";
_alv[2] = "\\";
_alv[3] = "|";
_alv[0] = "/";
bar_make(1);
}
function bar_make(num, n, per, dot, all, per_str, alv_str, dot_str, sec){
_bar_num = num;
per_str = sprintf("%5.1f% ", num / _bar_per_all * 100);
dot = int(per_str / 100 * _bar_dot_all)
_bar_cnt = ++_bar_cnt % 4;
alv_str = _alv[_bar_cnt];
if(_bar_num == 1 || dot != _bar_dot){
_bar_dot = dot;
_bar_dot_s = "";
_bar_dot_e = "";
for(n=0;n<dot; ++n)
_bar_dot_s = _bar_dot_s"=";
for(n = dot+1; n <_bar_dot_all; ++n)
_bar_dot_e = _bar_dot_e".";
}
if(+per_str == 100) alv_str="";
dot_str = _bar_dot_s alv_str _bar_dot_e;
sec = systime() - _bar_sec_sys;
if(_bar_num ==1 || sec != _bar_sec){
_bar_sec = sec
if(sec < 3600){
_bar_sec_str = sprintf("%02d:%02d", int(sec/60), sec%60);
}else{
_bar_sec_str = sprintf("%02d:%02d:%02d", int(sec/3600),
int( (sec%3600)/60), sec%60);
}
}
printf("\r%s[%s] %s", per_str, dot_str, _bar_sec_str);
if(BAR_DEBUG_TEN){
if(_bar_per_str != "" && +per_str != 100)
if(per_str%10==0 && _bar_per_str != per_str)
print "";
}
_bar_per_str = per_str;
}
function bar(num){
if(num - _bar_num < _bar_stp) return;
bar_make(num);
}
FNR==1{
print " Read " FILENAME " ... (for lines only by wc utility)"
cmd="wc -l " FILENAME;
cmd | getline line;
print " " FILENAME " has " (+line) " lines"
close(cmd);
}
FNR == 1{
bar_set(+line);
}
{
bar(FNR)
# do something by yourself
#$0=$2" "$3" "$4" "$5" "$6
CNT[$0]++
}
END{
print""
}
|
|