Bash - 按非第一個字元排序

Bash - 按非第一個字元排序

我想按第一列對文件進行排序,但必須從 5 個字元開始排序。我怎樣才能做到這一點?

我的文件:

"TTTTCTTACA"            1       1
"TTTTCTTACC"                    1
"TTTTCTTACT"    1       1
"TTTTCTTAGC"    1
"TTTTCTTATT"                    2
"TTTTCTTCAA"    1               1       1
"TTTTCTTCAG"    1               2       1
"TTTTCTTCAT"            1       2       2
"TTTTCTTCCT"                            2
"TTTTCTTCGG"                    2       2
"TTTTCTTCTA"                            1
"TTTTCTTCTG"            1
"TTTTCTTCTT"    1                       2
"TTTTCTTGAA"            1
"TTTTCTTGCT"    1               1       1
"TTTTCTTTAA"    1
"TTTTCTTTAG"            1       1
"TTTTCTTTCT"    1
"TTTTCTTTGC"    1
"TTTTCTTTGG"            1       1
"TTTTCTTTGT"    1       1       2       1
"TTTTCTTTTA"    1

我正在嘗試:

sort -k1,1 file | uniq -s 6 -w 5 

當然,這是行不通的。也許排序有一些標誌,但我沒有找到它們。你有什麼想法嗎?

答案1

太長了;博士

sort -k1.5 file | uniq -s 6 -w 5


解釋

我的排序是 GNU coreutils 8.22。我的排序的線上說明頁顯示:

KEYDEF is F[.C][OPTS][,F[.C][OPTS]] for start and stop position, where F is a field number and  C
       a  character  position  in  the  field;  both are origin 1, and the stop position defaults to the
       line's end.

因此,對於目前的排序命令,sort -k1,1 file使用第一個單字到第一個單字作為排序。

你想要的是(無論如何對於排序命令):

sort -k1.5 file | uniq -s 6 -w 5

這將使用第一個單字的第五個字符,這就是您想要的。

答案2

$sort -k2 文件

"TTTTCTTCTA"                            1
"TTTTCTTCCT"                            2
"TTTTCTTACC"                    1
"TTTTCTTATT"                    2
"TTTTCTTCGG"                    2       2
"TTTTCTTCTG"            1
"TTTTCTTGAA"            1
"TTTTCTTACA"            1       1
"TTTTCTTTAG"            1       1
"TTTTCTTTGG"            1       1
"TTTTCTTCAT"            1       2       2
"TTTTCTTAGC"    1
"TTTTCTTTAA"    1
"TTTTCTTTCT"    1
"TTTTCTTTGC"    1
"TTTTCTTTTA"    1
"TTTTCTTCTT"    1                       2
"TTTTCTTCAA"    1               1       1
"TTTTCTTGCT"    1               1       1
"TTTTCTTCAG"    1               2       1
"TTTTCTTACT"    1       1
"TTTTCTTTGT"    1       1       2       1

$sort -k2 檔案 |唯一-f 1

"TTTTCTTCTA"                            1
"TTTTCTTCCT"                            2
"TTTTCTTACC"                    1
"TTTTCTTATT"                    2
"TTTTCTTCGG"                    2       2
"TTTTCTTCTG"            1
"TTTTCTTACA"            1       1
"TTTTCTTCAT"            1       2       2
"TTTTCTTAGC"    1
"TTTTCTTCTT"    1                       2
"TTTTCTTCAA"    1               1       1
"TTTTCTTCAG"    1               2       1
"TTTTCTTACT"    1       1
"TTTTCTTTGT"    1       1       2       1

相關內容