1

I am trying to understand something about the "sort" command and I am struggling. I would appreciate some help here.

I do have a file called "my_file.csv" with the following content (generated by myself to just play with "sort"):

Id,RandomNumber,UUID,RandomPassword,Letters,RandomWord
1,18934,42ab7251-daaf-4a10-8349-213fef22ce78,l5ZIkKjF8QGlN9GmZ9qd,AAA,keelboatman
2,22045,bfc9d720-e8f8-406a-9841-c02edc2500bb,EPq64i1LaYTsNbB0ts78,BBB,unbinds
3,14468,b2d0f699-66c1-4571-9228-66399c1329a5,t9CzSFBZd8dS9gN3J3mO,CCC,foaminess
4,9359,4bf377b6-1b89-474f-a34b-4e5ca55f03a1,G2dwlMDdbYPUOGX68mmg,DDD,filander
5,31147,d1d9a6d4-7a8e-4fd5-a2a2-a2915614a054,ZaMw16rF3t1OjIGFc6KM,AAA,runologist
6,9909,5313eec0-99ac-4ec7-a8cf-7ee8847f98d4,VeZhlqMEDvAderKvB8qk,BBB,upgathering
7,10228,7e2e7cdf-b38a-4014-8235-c6f351068a14,A9EmNnksYK0AE9DVQcEG,CCC,nondiaphanousness
8,22815,75962dfe-cfd1-42cb-b14d-96dd8a01a165,Qy1NyB3xnkuhEH1Gh7Pb,DDD,extirpate
9,12688,c25c9468-83f7-4751-82c6-c150e538207b,wDSlIMIne6hQ8B7OIYip,AAA,aldamine
10,4133,f683f78a-f627-48aa-91b2-6f449f56fb23,LhxaoVi1fV7kFhFwXATA,DDD,underhole

If I wanted to sort the content of this file using the fifth column, I would use the following:

$ tail -n +2 my_file.csv | sort -t, -k5,5
1,18934,42ab7251-daaf-4a10-8349-213fef22ce78,l5ZIkKjF8QGlN9GmZ9qd,AAA,keelboatman
5,31147,d1d9a6d4-7a8e-4fd5-a2a2-a2915614a054,ZaMw16rF3t1OjIGFc6KM,AAA,runologist
9,12688,c25c9468-83f7-4751-82c6-c150e538207b,wDSlIMIne6hQ8B7OIYip,AAA,aldamine
2,22045,bfc9d720-e8f8-406a-9841-c02edc2500bb,EPq64i1LaYTsNbB0ts78,BBB,unbinds
6,9909,5313eec0-99ac-4ec7-a8cf-7ee8847f98d4,VeZhlqMEDvAderKvB8qk,BBB,upgathering
3,14468,b2d0f699-66c1-4571-9228-66399c1329a5,t9CzSFBZd8dS9gN3J3mO,CCC,foaminess
7,10228,7e2e7cdf-b38a-4014-8235-c6f351068a14,A9EmNnksYK0AE9DVQcEG,CCC,nondiaphanousness
10,4133,f683f78a-f627-48aa-91b2-6f449f56fb23,LhxaoVi1fV7kFhFwXATA,DDD,underhole
4,9359,4bf377b6-1b89-474f-a34b-4e5ca55f03a1,G2dwlMDdbYPUOGX68mmg,DDD,filander
8,22815,75962dfe-cfd1-42cb-b14d-96dd8a01a165,Qy1NyB3xnkuhEH1Gh7Pb,DDD,extirpate

If I wanted to sort the same file using a second key, let's say the second column (which are numbers) I would use the following:

$ tail -n +2 my_file.csv | sort -t, -k5,5 -k2n,2
9,12688,c25c9468-83f7-4751-82c6-c150e538207b,wDSlIMIne6hQ8B7OIYip,AAA,aldamine
1,18934,42ab7251-daaf-4a10-8349-213fef22ce78,l5ZIkKjF8QGlN9GmZ9qd,AAA,keelboatman
5,31147,d1d9a6d4-7a8e-4fd5-a2a2-a2915614a054,ZaMw16rF3t1OjIGFc6KM,AAA,runologist
6,9909,5313eec0-99ac-4ec7-a8cf-7ee8847f98d4,VeZhlqMEDvAderKvB8qk,BBB,upgathering
2,22045,bfc9d720-e8f8-406a-9841-c02edc2500bb,EPq64i1LaYTsNbB0ts78,BBB,unbinds
7,10228,7e2e7cdf-b38a-4014-8235-c6f351068a14,A9EmNnksYK0AE9DVQcEG,CCC,nondiaphanousness
3,14468,b2d0f699-66c1-4571-9228-66399c1329a5,t9CzSFBZd8dS9gN3J3mO,CCC,foaminess
10,4133,f683f78a-f627-48aa-91b2-6f449f56fb23,LhxaoVi1fV7kFhFwXATA,DDD,underhole
4,9359,4bf377b6-1b89-474f-a34b-4e5ca55f03a1,G2dwlMDdbYPUOGX68mmg,DDD,filander
8,22815,75962dfe-cfd1-42cb-b14d-96dd8a01a165,Qy1NyB3xnkuhEH1Gh7Pb,DDD,extirpate

Now I go deeper in detail and try to sort the same content by the fifth column and using the 2nd and 3rd digit of the RandomNumber column. For that I would use something like this:

$ tail -n +2 my_file.csv | sort -t, -k5,5 -k2.2n,2.3
5,31147,d1d9a6d4-7a8e-4fd5-a2a2-a2915614a054,ZaMw16rF3t1OjIGFc6KM,AAA,runologist
9,12688,c25c9468-83f7-4751-82c6-c150e538207b,wDSlIMIne6hQ8B7OIYip,AAA,aldamine
1,18934,42ab7251-daaf-4a10-8349-213fef22ce78,l5ZIkKjF8QGlN9GmZ9qd,AAA,keelboatman
2,22045,bfc9d720-e8f8-406a-9841-c02edc2500bb,EPq64i1LaYTsNbB0ts78,BBB,unbinds
6,9909,5313eec0-99ac-4ec7-a8cf-7ee8847f98d4,VeZhlqMEDvAderKvB8qk,BBB,upgathering
7,10228,7e2e7cdf-b38a-4014-8235-c6f351068a14,A9EmNnksYK0AE9DVQcEG,CCC,nondiaphanousness
3,14468,b2d0f699-66c1-4571-9228-66399c1329a5,t9CzSFBZd8dS9gN3J3mO,CCC,foaminess
10,4133,f683f78a-f627-48aa-91b2-6f449f56fb23,LhxaoVi1fV7kFhFwXATA,DDD,underhole
8,22815,75962dfe-cfd1-42cb-b14d-96dd8a01a165,Qy1NyB3xnkuhEH1Gh7Pb,DDD,extirpate
4,9359,4bf377b6-1b89-474f-a34b-4e5ca55f03a1,G2dwlMDdbYPUOGX68mmg,DDD,filander

Now, what I am trying to understand now is how does something like this work:

$ tail -n +2 my_file.csv | sort -t, -k5,5 -k2.4n,2.1

or

$ tail -n +2 my_file.csv | sort -t, -k5,5 -k5.3,4.3

In both cases the beginning of the second key is AFTER the end of the key (2.4 vs 2.1 in one case and 5.3 vs 4.3 in the other).

I would like to understand how the following cases work:

  • Key beginning after key ending (e.g: -k2.4n,2.1)
  • Key beginning in a field and ending in a field that appears before (e.g: -k6,5)
  • Key beginning in a character within a field and ending in a character of a field that appears before (e.g: -k6.4,5.3)

Thanks a lot in advance!

3
  • The n goes at the end of the argument to -k (-k 2.4,2.1n). Your sort may have --debug option.
    – Kusalananda
    Apr 18, 2022 at 11:04
  • Here are some comments about key-end interpretation.
    – SergA
    Apr 18, 2022 at 11:51
  • Hey people! thanks a lot for your answers. I went a bit deeper on the documentation and it's exactly what thanasisp mentioned in his answer. I found however very interesting the link that you posted @SergA, straight to "The Truth" :) Apr 19, 2022 at 4:17

1 Answer 1

1

A sort key that does not define a valid range, from left to right, will be ignored.

This is what we see in your testing and also defined into the POSIX manual:

       −k field_start[type][,field_end[type]]

   shall  define a key field that begins at field_start and ends at field_end inclusive, unless field_start falls
   beyond the end of the line or after field_end, in which case the key field is empty.

If you use --debug you will see the message ^ no match for key for every line, pointing to the start position of the invalid key, indicating that nothing is changing based on that key.

In your example, there is an earlier valid key, that will be used alone. Or any other sequence of valid keys, before or after the empty key, would be executed. And, from man sort:

If no key is given, use the entire line as the key. 

You must log in to answer this question.

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