![如何排序以便 caml 位于 xml 中的 c 之后?](https://linux22.com/image/1299561/%E5%A6%82%E4%BD%95%E6%8E%92%E5%BA%8F%E4%BB%A5%E4%BE%BF%20caml%20%E4%BD%8D%E4%BA%8E%20xml%20%E4%B8%AD%E7%9A%84%20c%20%E4%B9%8B%E5%90%8E%EF%BC%9F.png)
按行对文件进行排序
<LexerType name="caml" desc="Caml" ext="">
<LexerType name="c" desc="C" ext="">
导致 caml 位于 c 之前,我该如何将 c 放在第一位,而不依赖于此文件的字符位置?
答案1
使用sort
$ sort -t\" -k2,2 file.xml
<LexerType name="c" desc="C" ext=""/>
<LexerType name="caml" desc="Caml" ext=""/>
-k, --key=POS1[,POS2]
start a key at POS1, end it at POS2 (origin 1)
-t, --field-separator=SEP
use SEP instead of non-blank to blank transition
使用vim
:%sort /name=/
或者
:%sort r /name="\w*"/
:[range]sor[t][!] [i][u][r][n][x][o] [/{pattern}/]
When /{pattern}/ is specified and there is no [r] flag
the text matched with {pattern} is skipped, so that
you sort on what comes after the match.
Instead of the slash any non-letter can be used.
For example, to sort on the second comma-separated
field: >
:sort /[^,]*,/
To sort on the text at virtual column 10 (thus
ignoring the difference between tabs and spaces): >
:sort /.*\%10v/
To sort on the first number in the line, no matter
what is in front of it: >
:sort /.*\ze\d/
With [r] sorting is done on the matching {pattern}
instead of skipping past it as described above.
For example, to sort on only the first three letters
of each line: >
:sort /\a\a\a/ r