2

I am following this script and trying to understand what happens at each line. In the following line extracted from here it involves sorting some fields. In the 14th example given here it says -k2,5 stand for sorting column 2 and 5 which are numeric values and -k9 stand for sorting of column 9 which is a non-numeric value column.

# Process the STMs
    cat db/TEDLIUM_release1/$set/stm/*.stm | sort -k1,1 -k2,2 -k4,4n | \
      sed -e 's:<F0_M>:<o,f0,male>:' \
          -e 's:<F0_F>:<o,f0,female>:' \
          -e 's:([0-9])::g' \
          -e 's:<sil>::g' \
          -e 's:([^ ]*)$::' | \
      awk '{ $2 = "A"; print $0; }'
  } | local/join_suffix.py db/TEDLIUM_release1/TEDLIUM.150K.dic > data/$set/stm 

But in the code segment above (sort -k1,1 -k2,2 -k4,4n), it maps -k1,1 and it has 3 set of those as well. Can someone help me to understand this?

1 Answer 1

5

From man sort:

-k, --key=POS1[,POS2]
       start a key at POS1 (origin 1), end it at POS2 (default end of line)
...

POS  is F[.C][OPTS], where F is the field number and C the character position
in the field; both are origin 1.  If neither -t nor -b is in effect,
characters in a field are counted from the beginning of the preceding
whitespace.  OPTS is one or more single-letter  ordering options, which
override global ordering options for that key.  If no key is given, use the
entire line as the key.

The 14th example in that link you posted is simply incorrect. From the above manpage excerpt it's pretty clear that -k2,5 will not sort "based on keys 2 and 5" but based on fields 2 through 5, counted all together as a single sorting key.

(As an aside: Code examples from random online sources are well and good for getting a rough idea of what the command is for or can do, but when you want to dig in and really understand what goes on, there's no substitute for reading—or at least consulting—the man page.) ;)

5
  • Furthermore the linked example uses -t "," so there is only one field... Commented Jan 16, 2016 at 9:10
  • @StephenKitt, good spotting. That makes the article in question particularly nasty, since it not only has broken code examples, it has animated images showing those code examples working, which they don't. (I say "those"; the 14th at least is broken.)
    – Wildcard
    Commented Jan 16, 2016 at 9:13
  • The animation seems accurate at least for the 14th example: the output isn't sorted... That doesn't seem to bother the author though! Commented Jan 16, 2016 at 9:18
  • so, can you please explain what it really happens with my example? Still I really don't get what it is meant by -k,1 and why we there are three of them. (In my case does it mean it start from position 1 and end at position 1, which means it consider whatever the character at position 1? )
    – udani
    Commented Jan 16, 2016 at 9:20
  • 1
    Each -k parameter defines a set of fields, not characters, to sort by (simplifying slightly, fields are separated by blank characters by default). They are used in succession to decide how two lines should be sorted: in this case, if the values in the first field are different, they determine the order, otherwise the second, and finally the fourth, and values in the fourth field are compared numerically. Commented Jan 16, 2016 at 13:46

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .