← 返回 nvidia 的题目列表Linux Shell Scripting Task (Text/Log Processing)
类型:online_judge
Problem: Linux Shell Scripting Task (Text/Log Processing)
In a Linux environment, you are given a log file log.txt. Each line has the following format (space-separated fields):
<timestamp> <level> <module> <message...>
Where:
timestamp is a no-space string (e.g., 2026-05-14T10:00:00Z)
level is one of INFO / WARN / ERROR
module is a no-space string (e.g., pcie, memory)
message is the remaining part (may contain spaces)
Write a shell script that reads the log from stdin and outputs the number of ERROR lines per module, sorted by ERROR count descending; if counts tie, sort by module lexicographically ascending.
Input
stdin: the whole log content (multiple lines).
Output
Multiple lines, each formatted as:
<module> <error_count>
Constraints
Number of lines N: 1 ≤ N ≤ 2e5
Must run on a typical GNU/Linux environment
Tests
Input:
2026-05-14T10:00:00Z INFO pcie link up
2026-05-14T10:01:00Z ERROR pcie link down
2026-05-14T10:02:00Z ERROR memory ecc error
2026-05-14T10:03:00Z WARN memory corrected
2026-05-14T10:04:00Z ERROR pcie timeout
Output:
pcie 2
memory 1
Input:
2026-05-14T10:00:00Z INFO a ok
2026-05-14T10:01:00Z WARN a warn
Output:
Input:
2026-05-14T10:00:00Z ERROR z bad
2026-05-14T10:00:01Z ERROR a bad
Output:
a 1
z 1
Input:
2026-05-14T10:00:00Z ERROR gpu X
2026-05-14T10:00:01Z ERROR gpu Y
2026-05-14T10:00:02Z ERROR gpu Z
Output:
gpu 3
Input:
2026-05-14T10:00:00Z ERROR m1 a
2026-05-14T10:00:01Z INFO m1 b
2026-05-14T10:00:02Z ERROR m2 c
2026-05-14T10:00:03Z ERROR m2 d
2026-05-14T10:00:04Z ERROR m3 e
Output:
m2 2
m1 1
m3 1
Example
Input
2026-05-14T10:00:00Z INFO pcie link up
2026-05-14T10:01:00Z ERROR pcie link down
2026-05-14T10:02:00Z ERROR memory ecc error
2026-05-14T10:03:00Z WARN memory corrected
2026-05-14T10:04:00Z ERROR pcie timeout
Output
pcie 2
memory 1