Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/vim/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sorting 对文本中的特定行进行排序_Sorting_Vim_Emacs_Vi_Nano - Fatal编程技术网

Sorting 对文本中的特定行进行排序

Sorting 对文本中的特定行进行排序,sorting,vim,emacs,vi,nano,Sorting,Vim,Emacs,Vi,Nano,我有给定的文本: # Blub Hello this is a blub text. # Bla This is the bla text. # Abba Another text. 是否可以使用#对行进行排序?因此,生成的文本是: # Abba Another text. # Bla This the bla text. # Blub Hello this is a blub text. 最好使用vim或emacs。类似于: :sort! /^#.+\n.+\n$/ 我不确定线

我有给定的文本:

# Blub
Hello this is a blub text.

# Bla
This is the bla text.

# Abba
Another text.
是否可以使用
#
对行进行排序?因此,生成的文本是:

# Abba
Another text.

# Bla
This the bla text.

# Blub
Hello this is a blub text.
最好使用vim或emacs。

类似于:

:sort! /^#.+\n.+\n$/ 

我不确定线路分段顺序

您没有这样标记,但我认为
awk
是这项工作的最佳工具。使用
gawk
进行以下工作:

gawk RS='\n\n' '{ 
  gsub("\n$", "")
  gsub("\n", "@")
  print 
}' file_to_be_sorted | sort | sed -e 's/$/\n/' -e 's/@/\n/'
解释 通过将记录分隔符(RS)设置为“\n\n”
gawk
,可以从每个块创建记录。将每条记录转换为一行,并使用
@
作为分隔符(
gsub(“\n”,“@”)
),此时正常的
排序工作<然后使用代码>sed
重新创建块
gsub(“\n$”,“”)
修复了最后一条记录的空白问题

注意:如果任何块包含
@
,则需要选择不同的分隔符。

在Emacs中

  • M-x
    sort regexp字段
  • 输入:
    。[^.]*
  • 输入:
    \&
  • 第一个regexp分隔记录,第二个指定排序键

    如果您可以自由选择标记字符并使用
    *
    而不是
    #
    ,则可以使用组织模式的命令
    org sort entries
    ,从而避免您输入regexp