How to sort with multiple lines in bash?

前端 未结 3 714
太阳男子
太阳男子 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:28

    Not sure if it's going to work for you but with some limitations here is a line that kind of doing what you need.

    awk '{if ((NR%2-1)==0) {line=sprintf("%-30s",$0)} else {print line ":" $0}}' | \
      sort --key=1,30 | tr ':' '\n'
    

    Assumptions: There are no blank lines in between records, name is always less than 30 characters and there is no : used in text.

    I am sure you can figure how to change it if assumptions are different.

    In a nutshell it, merges two lines using ':' as separator, pads first line to 30 characters and sorts using first 30 characters. Then it breaks lines back.

提交回复
热议问题