How to sort with multiple lines in bash?

前端 未结 3 721
太阳男子
太阳男子 2021-01-02 09:50

I am trying to sort a list of names followed by another string such as:

John Doe
AVAIL

Sara Doe
CALL

Jim Doe
AVAIL

I am trying to sort th

3条回答
  •  半阙折子戏
    2021-01-02 10:21

    not directly, but you can use some intermediate form like this. i am assuming that your value (CALL, AVAIL, etc.) is limited. otherwise you need to use patterns that are more complicated, but it can be done. actually anything can be done in bash :-)

    cat sorting | sed -n '1h; 1!H; ${ g; s/\nCALL\n/::CALL::/g; s/\nAVAIL\n/::AVAIL::/g ; s/\nAVAIL/::AVAIL::/g p }' | sort | sed "s/::/\n/g"
    Jim Doe
    AVAIL
    
    John Doe
    AVAIL
    
    Sara Doe
    CALL
    

提交回复
热议问题