0

I want to understand the exact meaning of pos1 and pos2 under sort -k

According to the manual

-k, --key=POS1[,POS2]

See POS syntax below.Start a key at POS1 (origin 1), end it at POS2 (default end of line).

What exactly do pos1 and pos2 mean?

Could anyone explain it clearly in simple terms?

Thanks!

2

1 Answer 1

2

Maybe this will help show the difference:

$ cat lines.txt
c 1 a
b 2 a
c 3 c
a 3 a
a 2 c
a 1 b
b 1 c
c 2 b
b 3 b

Sorting by second field (unrestricted):

$ sort -k2 lines.txt
c 1 a
a 1 b
b 1 c
b 2 a
c 2 b
a 2 c
a 3 a
b 3 b
c 3 c

Sorting by second field and restricted to second field:

$ sort -k2,2 lines.txt
a 1 b
b 1 c
c 1 a
a 2 c
b 2 a
c 2 b
a 3 a
b 3 b
c 3 c

Notice the change in the third field.

You must log in to answer this question.

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