如何对使用grep -c获得的输出进行排序?

时间:2021-06-22 19:31:28

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

我使用以下'grep'命令来获取给定路径中每个文件中字符串警报的计数:

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.

您也可以*地建议除grep之外的命令。

1 个解决方案

#1


22  

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.

把它管成排序。假设您的文件名没有冒号,请使用“-t”选项将冒号指定为字段saparator。使用-n进行数字排序。

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).

应将行拆分为以“:”分隔的字段,使用第二个字段进行排序,并将其视为数字(因此21实际上晚于3)。

#1


22  

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.

把它管成排序。假设您的文件名没有冒号,请使用“-t”选项将冒号指定为字段saparator。使用-n进行数字排序。

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).

应将行拆分为以“:”分隔的字段,使用第二个字段进行排序,并将其视为数字(因此21实际上晚于3)。