試試這個:
perl -ne 's/^(?!!!!)/###/g; print;' file | sed ':a;N;$!ba;s/\n###/###/g' | sort | sed 's/###/\n/g'
第一標記所有非標題行:
perl -ne 's/^(?!!!!)/###/g; print;' file
!!! Better Heading
###This section has a sub-heading
###!! Sub-Heading one
###
!!! Can't think of another one
###umm...
###
###
!!! A Great Heading
###Some text here
然後取出\n
befor這些行:
perl -ne 's/^(?!!!!)/###/g; print;' file | sed ':a;N;$!ba;s/\n###/###/g'
!!! Better Heading###This section has a sub-heading###!! Sub-Heading one###
!!! Can't think of another one###umm...######
!!! A Great Heading###Some text here
然後進行排序,並與\n
替換標記:
perl -ne 's/^(?!!!!)/###/g; print;' file | sed ':a;N;$!ba;s/\n###/###/g' | sort | sed 's/###/\n/g'
!!! A Great Heading
Some text here
!!! Better Heading
This section has a sub-heading
!! Sub-Heading one
!!! Can't think of another one
umm...
另請參閱http://superuser.com/questions/752032/how-do-i-sort-multiple-blocks-of-text-by-the-first-line-in-each-block-in -vim在Vim內部提供解決方案。 –