How to sort on column for rows containing a certain word

前端 未结 4 957
耶瑟儿~
耶瑟儿~ 2021-02-02 18:32

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:



        
4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-02 18:40

    Sort by 2nd column by selecting it in visual mode (e.g. Control+v), then run:

    !sort
    

    or to sort by 3rd column

    sort -k 3 
    

    or

    :sort /.*\%3v/
    

    Alternatively select the lines you wish to sort using the Shift+V command. Then enter

    !sort -k 3n
    

    or use the below code to tell Vim to skip the first two words in every line and sort on whatever follows:

    :%sort /^\S\+\s\+\S\+\s\+/ 
    

    or i.e. sort by 8th line:

    :sort /.*\%55v/
    

    The 'virtual' specification is the absolute number of column , which treats spaces + tabs as single character (shortly, it doesn't count tabs as eight spaces),

    so to sort by last column:

    :%sort /\<\S\+\>$/ r
    

提交回复
热议问题