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

我想按字母顺序对文本块进行排序,从下一个开始!!!并在下一个之前完成!!!

有没有办法让我这样做?

4

1 回答 1

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然后在这些行之前删除:

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...
于 2014-05-20T04:53:00.920 回答