2

I want to be able to sort the output from the grep -rc command,

I am currently using the command:

grep -rc "Pattern" . | sort -t: -rk2,2n

however the list is still sorted from low to high, instead of high to low... it also prints out the exact output when using:

grep -rc "Pattern" . | sort -t: -k2,2n

So what I want is for the outputs:

data_x:12 
data_y:34
data_z:56
data_a:205
data_b:1003

to be sorted into

data_b:1003
data_a:205
data_z:56
data_y:34
data_x:12
1
  • 2
    Try grep -rc "Pattern" . | sort -t: -k2,2rn
    – heemayl
    Feb 12, 2016 at 22:53

2 Answers 2

3

You are almost there except you need the r modifier do a reverse sort:

grep -rc "Pattern" . | sort -t: -k2,2nr

check the r modifier after n.

1

You can use this,

~]# cat test
 data_x:12
 data_y:34
 data_z:56
 data_a:205
 data_b:1003

~]# sort -k2 -n -r -t: test
 data_b:1003
 data_a:205
 data_z:56
 data_y:34
 data_x:12

So for your code, you can do

~]# grep -rc "Pattern" . | sort -k2 -n -r -t:

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .