user742004
user742004

Reputation: 151

How to sort the files based on the grep count?

my grep -c command for a particular pattern returns files as follows

A:2
B:6
c:1
d:9

Now i want to sort the files based on this command. so my final op will be

c:1
A:2
B:6
d:9

how to use grep and sort together?

Upvotes: 6

Views: 5551

Answers (2)

bmk
bmk

Reputation: 14147

I would do it like this:

grep -c $pattern A B c d | sort -n -t: -k2

-n means numeric sort, -t: means that the column delimiter is : and -k2 means that the second column is considered for sorting.

Upvotes: 3

drysdam
drysdam

Reputation: 8637

grep -c <pattern> * | sort -n -k2 -t:

The -k2 changes the key field, the -t: sets the field separator to : (the -n means a numeric sort)

Upvotes: 9

Related Questions