Command 1: Shell code timeawk {print $1} access. log | sort | uniq-c | sort-nr | head-578129192.168.13.16675165192.168.12.5258657192.168.12.19954146192.168.13.18334310192.168.13.181real0m5.849suser0m5.566ssys0m0.285s command 2
Command 1: Shell code time awk '{print $1}' access. log | sort | uniq-c | sort-nr | head-5 78129 192.168.13.166 75165 192.168.12.52 58657 192.168.12.199 54146 192.168.13.183 34310 192.168.13.181 real 0m5. 849 s user 0m5. 566 s sys 0m0. 285 s command 2
Command 1:
Shell code
Time awk '{print $1}' access. log | sort | uniq-c | sort-nr | head-5
78129 192.168.13.166
75165 192.168.12.52
58657 192.168.12.199
54146 192.168.13.183
34310 192.168.13.181
Real 0m5. 849 s
User 0m5. 566 s
Sys 0m0. 285 s
Command 2:
Shell code
Time awk '{a [$1] ++} END {for (I in a) {printf ("% d \ t % s \ n", a [I], i)} 'access. log | sort-nr | head-5
78129 192.168.13.166
75165 192.168.12.52
58657 192.168.12.199
54146 192.168.13.183
34310 192.168.13.181
Real 0m0. 626 s
User 0m0. 549 s
Sys 0m0. 080 s
The preceding twoStatisticsTop Command, the comparison result is clear at a glance. Command 1 is convenient and efficient; command 2 is troublesome and efficient.
The reason why I thought of comparing these two commands was due to an interview. At that time, my answer was command 2 (I used to use command 2 before that, but I did not use command 1 ). The other party immediately questioned the answer I gave. First, I doubt the correctness of the answer. "Isn't this method correct ?". When I explained that I have used this command in my work, I questioned the efficiency of this command. "There should be more efficient methods !".
What I actually want to say is that sometimes the interview may not be able to identify the real problem.
[Editor: peter]