2014-05-20 64 views
1

我有一個標記語法爲!!!的降價頁面。例如:使用正則表達式按字母順序排列標題

!!! Better Heading 
This section has a sub-heading 
!! Sub-Heading one 

!!! Can't think of another one 
umm... 


!!! A Great Heading 
Some text here 

我想按字母順序有沒有辦法爲我做這樣的文本塊進行排序,開始在!!!和先下一!!!

整理?

+1

另請參閱http://superuser.com/questions/752032/how-do-i-sort-multiple-blocks-of-text-by-the-first-line-in-each-block-in -vim在Vim內部提供解決方案。 –

回答

2

試試這個:

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... 
+1

謝謝,我不得不調整你的正則表達式才能在vim中工作,但這個想法是一樣的。這是我最終做的一個宏':%s/^ \(!!! \)\ @!/ ###/g :%s/\ n ###/###/g :sort :%s/###/\ r/g ' –