LINUX · Text tools · 8 min
Summarize log levels
Combine field selection, sorting, and counting.
Not completedYour task
Create levels.txt with a count for each first-column log level. Keep events.log unchanged.
What you’re aiming for
- ERROR has count 2, INFO has count 3, and WARN has count 1.
- The original six events remain.
1. Prepare your practice folder
Use a Linux terminal, such as Ubuntu or WSL on Windows. These examples use Linux file permissions. In WSL, create your practice folder in your Linux home directory (~).
Create a new, empty folder for this exercise. If that name already exists, choose another new name. Continue only after you’re inside the new folder.
mkdir opsrung-linux-count-log-levels && cd opsrung-linux-count-log-levels
Run these setup commands in that folder:
printf '%s\n' 'INFO ready ERROR disk INFO request WARN retry ERROR denied INFO done' > events.log
2. Try the task
Work toward the goal above. Start by inspecting the files or resources, then use a hint if you need one.
Hint 1
awk can print the first whitespace-separated field.
Hint 2
uniq -c counts adjacent repeated lines, so sort first.
Hint 3
Use awk, sort, and uniq -c as a pipeline.
3. Review your result
Use these commands to look at your result and compare it with the goals. The website does not run or verify them.
cat levels.txt wc -l events.log
What you should see
- ERROR has count 2, INFO has count 3, and WARN has count 1.
- The original six events remain.
If your result differs: Compare the actual output with each goal. Keep your work inside this exercise’s folder and use only its named resources.
Compare with one possible solution
awk '{print $1}' events.log | sort | uniq -c > levels.txtOther approaches are fine if you reach the same goal. Opening this solution does not mark the exercise complete.
Finished this exercise?
You decide when you’re done. Your choice is saved in this browser, and you can change it later.