14

I use the following 'grep' command to get the count of the string alert in each of my files at the given path:

grep 'alert' -F /usr/local/snort/rules/* -c

How do I sort the resulting output in desired order- say ascending order, descending order, ordered by name, etc. An answer specific to these cases is sufficient.

You may freely suggest a command other than grep as well.

pnp
  • 544
  • 3
  • 6
  • 21

1 Answers1

30

Pipe it into sort. Assuming your filenames have no colons, use the "-t" option to specify the colon as field saparator. Use -n for numerical sorting.

Example:

grep 'alert' -F /usr/local/snort/rules/* -c | sort -t: -n -k2

should split lines into fields separated by ":", use the second field for sorting, and treat this as numbers (so 21 is actually later than 3).

Christian Stieber
  • 9,706
  • 22
  • 21