How to sort on column for rows containing a certain word

vim

Solution

Pipe your input to an external command:

:%!grep sdf | sort -n -k3

Details:

- select the whole content using '%'

- pipe it to an external command using '!'

- grep onyl the lines containing 'sdf'

- sort these lines numerically (-n) on the third field (-k3)

Problem

I want to sort on a certain column only for rows containing a certain word. I don't want to see rows not containing that word. For example I have this text file: ``` sdf ggfds 7 sdf sgs 5 sdf dfgs 3 foo dffg 2 bar dffg 2 sdf sddfg 4 ``` I want to sort 3rd column for rows containing only "sdf" word (doesnt have to be in a first column) and I want to see this output: ``` sdf dfgs 3 sdf sddfg 4 sdf sgs 5 sdf ggfds 7 ```

Original source