Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

shell - Sorting by two substrings of the same column with only one reversed

I have a file that looks like this:

2836344588  ...  ...  N  fs1q
2836344589  ...  ...  N  fp2q
2836374222  ...  ...  N  fp3q
2836374223  ...  ...  N  fp6q
2836374224  ...  ...  N  fp7q
2836384836  ...  ...  N  fs2q
2836384837  ...  ...  N  fp1q
2836384838  ...  ...  N  fp4q
2836384839  ...  ...  N  fp5q
2836384957  ...  ...  N  fs3q
2836384958  ...  ...  N  fs7q
2836404416  ...  ...  N  fs5q
2836414186  ...  ...  N  fs6q
2836434267  ...  ...  N  fs4q

I would like to sort it by the second and the third characters of the fifth column: sorting on the second character should be done in reverse alphabetic order (s should come before p) and sorting on the third character should be done in a regular (i.e. non-reversed) numeric order.

I thought I could achieve this with:

sort -k5.2r -k5.3n

but this results in:

2836384958  ...  ...  N  fs7q
2836414186  ...  ...  N  fs6q
2836404416  ...  ...  N  fs5q
2836434267  ...  ...  N  fs4q
2836384957  ...  ...  N  fs3q
2836384836  ...  ...  N  fs2q
2836344588  ...  ...  N  fs1q
2836374224  ...  ...  N  fp7q
2836374223  ...  ...  N  fp6q
2836384839  ...  ...  N  fp5q
2836384838  ...  ...  N  fp4q
2836374222  ...  ...  N  fp3q
2836344589  ...  ...  N  fp2q
2836384837  ...  ...  N  fp1q

where sorting on the third character of the fifth column is also reversed.

How can I sort this file so that that reverse sorting happens only on the second character (all 's' lines come before all the 'p' lines) but not on the third character? In other words, how do I get to this desired output:

2836344588  ...  ...  N  fs1q
2836384836  ...  ...  N  fs2q
2836384957  ...  ...  N  fs3q
2836434267  ...  ...  N  fs4q
2836404416  ...  ...  N  fs5q
2836414186  ...  ...  N  fs6q
2836384958  ...  ...  N  fs7q
2836384837  ...  ...  N  fp1q
2836344589  ...  ...  N  fp2q
2836374222  ...  ...  N  fp3q
2836384838  ...  ...  N  fp4q
2836384839  ...  ...  N  fp5q
2836374223  ...  ...  N  fp6q
2836374224  ...  ...  N  fp7q

Many thanks in advance.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Because the reverse flag applies to all -k in that command, you could pipe it through two sort commands instead of trying to do them at the same time? (I saved the text in the file s.txt). It also worked better for me with the -b option...?

[I NEEDED the -s option as well to make it not keep sorting beyond the specified field.]

sort s.txt -n -k5.3,5.3 -b | sort -r -k5.2,5.2 -b -s

Output sorted by column 2 in reverse, then within that numerically (I modified the input file to be more variable). Needed to specify start and stop column with ,. So now in the 5th field, the second column in sorted t->p and the third column 1->9.

2836384837  ...  ...  N  ft1q
2836404416  ...  ...  N  ft5q
2836414186  ...  ...  N  ft6q
2836344588  ...  ...  N  bs1q
2836384836  ...  ...  N  fs2q
2836384957  ...  ...  N  fs3q
2836434267  ...  ...  N  fs4q
2836384958  ...  ...  N  cs7q
2836344589  ...  ...  N  fp2q
2836374222  ...  ...  N  fp3q
2836384838  ...  ...  N  bp4q
2836384839  ...  ...  N  fp5q
2836374223  ...  ...  N  ap6q
2836374224  ...  ...  N  fp7q

In this case, specifying the position twice and using the -s option will make sure it is limiting the sort to that column and not continuing to the end of the line.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...