A book about the command line for humans.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

30 lines
580 B

  1. #!/usr/bin/env bash
  2. # Optionally specify number of lines to show, defaulting to 10:
  3. TOSHOW=10
  4. CASEOPT=""
  5. while getopts ":in:" opt; do
  6. case $opt in
  7. i)
  8. CASEOPT="-i"
  9. ;;
  10. n)
  11. TOSHOW=$OPTARG
  12. ;;
  13. \?)
  14. echo "Invalid option: -$OPTARG" >&2
  15. exit 1
  16. ;;
  17. :)
  18. echo "Option -$OPTARG requires an argument." >&2
  19. exit 1
  20. ;;
  21. esac
  22. done
  23. # sort and then uniqify STDIN,
  24. # sort numerically on the first field,
  25. # chop off everything but $TOSHOW lines of input
  26. sort < /dev/stdin | uniq -c $CASEOPT | sort -k1 -nr | head -$TOSHOW