sorted-status.txt is sorted so identical status names are adjacent.
Display each status and its consecutive occurrence count.
Which command is appropriate?
uniq -c collapses adjacent identical lines and prefixes each group with its count.
Detailed explanation
uniq -c sorted-status.txtCorrect. uniq -c groups adjacent equal lines and prints their counts.
sort -c sorted-status.txtIncorrect. sort -c checks ordering but does not count groups.
wc -l sorted-status.txtIncorrect. wc -l reports only the total line count.
uniq -d sorted-status.txtIncorrect. uniq -d prints duplicate lines but does not show their counts.
Try it yourself
An example you can run in a temporary verification environment.
printf 'down\ndown\nup\nup\nup\n' | uniq -cExpected result
2 downと3 upKey points
- uniq handles adjacent duplicates
- -c prints counts
- Sort first when needed
Notes
- Environment: GNU coreutils 9.x / 標準入力
- Use small, disposable text streams when testing filters.
- Locale, encoding, and GNU/POSIX implementation details can affect edge cases.
Foundation review
Place the operation
uniq counts runs, not all equal lines scattered throughout an unsorted file.
Check the side effect
Compare results before and after sorting a temporary input.