Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

Sorting a file with lines

<LexerType name="caml" desc="Caml" ext="">
<LexerType name="c" desc="C" ext="">

causes caml to be before c, how do I put c first in general, without relying on the character positions of this file?

share|improve this question
1  
What program are you using to sort the lines, and how are you invoking it? Just doing a bytewise comparison should get you the results you want. –  jwodder Dec 23 '11 at 21:31
    
I'm using sort (GNU coreutils) 8.5. –  nnn Dec 23 '11 at 21:34
    
For me, just running sort on those two lines causes the "c" line to be printed before the "caml" line, so something else must be going on. Does the "caml" line contain extra leading whitespace in your file? –  jwodder Dec 23 '11 at 21:37
    
Yes, both lines contain the same amount of leading spaces. –  nnn Dec 23 '11 at 21:42
1  
Try adding LC_ALL=C before sort invocation, e.g. "LC_ALL=C sort ...", maybe it's a locale issue, thus you're getting weird sort order. –  haimg Dec 23 '11 at 21:42

1 Answer 1

up vote 0 down vote accepted

using 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

using vim

:%sort /name=/

or

:%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
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.