我试图理解有关“ sort
”命令的一些内容,但我很挣扎。我希望在这里得到一些帮助。
我确实有一个名为“”的文件,my_file.csv
其中包含以下内容(由我自己生成,只是为了玩“ 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
如果我想使用第五列对该文件的内容进行排序,我将使用以下内容:
$ 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
如果我想使用第二个键对同一文件进行排序,假设第二列(数字)我将使用以下内容:
$ 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
现在,我更深入地了解细节,并尝试按第五列并使用该列的第二个和第三个数字对相同的内容进行排序RandomNumber
。为此,我会使用这样的东西:
$ 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
现在,我现在想了解的是这样的事情是如何工作的:
$ tail -n +2 my_file.csv | sort -t, -k5,5 -k2.4n,2.1
或者
$ tail -n +2 my_file.csv | sort -t, -k5,5 -k5.3,4.3
在这两种情况下,第二个键的开头都在该键的结尾之后(一种情况下为2.4
vs ,另一种情况下为vs )。2.1
5.3
4.3
我想了解以下情况如何运作:
- 键开始后键结束(例如:
-k2.4n,2.1
) - 键以某个字段开始并以之前出现的字段结束(例如:
-k6,5
) - 键以字段内的字符开始,并以之前出现的字段的字符结束(例如:
-k6.4,5.3
)
预先非常感谢!
答案1
未定义从左到右的有效范围的排序键将被忽略。
这是我们在您的测试中看到的,并且也在 POSIX 手册中定义:
−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.
如果使用,--debug
您将在每一行看到一条消息^ no match for key
,指向无效键的开始位置,表明该键没有任何变化。
在您的示例中,有一个较早的有效密钥,将单独使用。或者在空键之前或之后执行任何其他有效键序列。并且,来自man sort
:
If no key is given, use the entire line as the key.