Text-Formatting

如何對齊列並反轉混亂的生物學表中的最後一個欄位

  • October 9, 2018

我有一個生成的表,數字搞砸了:

0.967662 0.850492  0.935517 325969
1.071937  0.976805 1.086638      526410.1
0.972091    0.871967     0.950352      306079
1.048607  0.925483  1.008793        412640.1
1.002087       0.888653   0.977475      188199
1.147052  1.029975       1.126825 285331.1
1.062427      0.960176      1.040016 282640.1
1.019481 0.886356 0.974687  980210.1
0.973101  0.857897 0.929414 588859

不僅如此,最後一列的數字也顛倒了。例如,325969.0必須實際上是0.969523.

結果必須是這樣的:

0.967662 0.850492 0.935517 969523
1.071937 0.976805 1.086638 1.014625
0.972091 0.871967 0.950352 970603
1.048607 0.925483 1.008793 1.046214
1.002087 0.888653 0.977475 991881
1.147052 1.029975 1.126825 1.133582
1.062427 0.960176 1.040016 1.046282
1.019481 0.886356 0.974687 1.012089
0.973101 0.857897 0.929414 958885

如何解決這個問題?

perl -lane '$n = reverse pop @F; print join " ", @F, $n' file
0.967662 0.850492 0.935517 969523
1.071937 0.976805 1.086638 1.014625
0.972091 0.871967 0.950352 970603
1.048607 0.925483 1.008793 1.046214
1.002087 0.888653 0.977475 991881
1.147052 1.029975 1.126825 1.133582
1.062427 0.960176 1.040016 1.046282
1.019481 0.886356 0.974687 1.012089
0.973101 0.857897 0.929414 958885

使用 awk 和輔助函式來反轉一個欄位:

function reverse(str) {
 trs=""
 for(i=length(str); i > 0; i--) {
   trs=trs substr(str, i, 1);
 }
 return trs
}
{
 $4=reverse($4);
 print;
}

將其保存到文件中,然後執行:awk -f that-file-above < input > output.

在您的範例輸入中,它會導致:

0.967662 0.850492 0.935517 969523
1.071937 0.976805 1.086638 1.014625
0.972091 0.871967 0.950352 970603
1.048607 0.925483 1.008793 1.046214
1.002087 0.888653 0.977475 991881
1.147052 1.029975 1.126825 1.133582
1.062427 0.960176 1.040016 1.046282
1.019481 0.886356 0.974687 1.012089
0.973101 0.857897 0.929414 958885

引用自:https://unix.stackexchange.com/questions/474298