Count records by the first field
Build a paste-ready command, then review its compatibility and effects before running it.
awk '{count[$1]++} END {for (key in count) print count[key], key}' <file> | sort -nr | headComplete required fields to copy the generated command.
read-onlyno known side effects Compatibility
LinuxVerified for Linux using posix, bash, zsh syntax.
Operational knowledgereview due 2027-01-20
Requirementsawkawk must be installed and available on PATH.
Version supportawk Current supported releasesVerified for linux using posix, bash, zsh syntax; consult compatibility notes for platform-specific differences.
Expected signals842 INFORepresentative successful output; values vary with the selected target and system state.
Known errorsawk: command not foundawk is missing or is not available on PATH.
Verifyawk '{count[$1]++} END {for (key in count) print count[key], key}' {{file}} | sort -nr | headThe output matches the expected target and exits without an error.Rollback noteNot required: this command is read-only and does not change system state.
Command breakdown
01awkCommandRuns the awk stage of this one-liner.
02'{count[$1]++} END {for (key in count) print count[key], key}'ArgumentPasses '{count[$1]++} END {for (key in count) print count[key], key}' to awk.
03<file>ParameterA value supplied in the Fill parameters section.
04|PipelinePasses the output on the left to the command on the right.
05sortCommandRuns the sort stage of this one-liner.
06-nrOptionConfigures sort with the -nr option.
07|PipelinePasses the output on the left to the command on the right.
08headCommandRuns the head stage of this one-liner.
Example input
awk '{count[$1]++} END {for (key in count) print count[key], key}' /var/log/syslog | sort -nr | head
Example output
842 INFO
42 WARN
3 ERROR
Illustrative output — exact values vary by system and data.
Official sources