Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort the files based on the grep count?

Tags:

grep

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?

like image 550
user742004 Avatar asked May 06 '11 15:05

user742004


2 Answers

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)

like image 85
drysdam Avatar answered Sep 24 '22 01:09

drysdam


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.

like image 36
bmk Avatar answered Sep 24 '22 01:09

bmk