Skip to main content
8 of 10
I wanted to add a \ to the code snippet so it could be copied and pasted, but there's an 8 character minimum on edits, so I removed the spaces and CR instead. EXTRA: you can divide lines on pipes, and the shell will understand
grochmal
  • 8.9k
  • 4
  • 34
  • 63

I would use tr instead of awk:

echo "Lorem ipsum dolor sit sit amet et cetera." |
tr '[:space:]' '[\n*]' |
grep -v "^\s*$" |
sort |
uniq -c |
sort -bnr
  • tr just replaces spaces with newlines
  • grep -v "^\s*$" trims out empty lines
  • sort to prepare as input for uniq
  • uniq -c to count occurrences
  • sort -bnr sorts in numeric reverse order while ignoring whitespace

wow. it turned out to be a great command to count swear-per-lines

find . -name "*.py" -exec cat {} ; | tr '[:space:]' '[\n*]' | grep -v "^\s*$" | sort | uniq -c | sort -bnr | grep fuck

seler
  • 596
  • 4
  • 5