0

I have a table that was generated and the numbers got messed up:

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

not only that, the numbers in the last column were reversed. For example, 325969.0 must actually be 0.969523.

The result must be like this:

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

How can this can be fixed?

  • 1
    The best way would be to fix the process that generates the data, not adding another software layer to repair it. – glenn jackman Oct 9 '18 at 16:35
  • Without having any context about the meaning of your data, the integers in the last column look suspicious. is 325969 supposed to be reversed to 969523 or should it be 0.969523 ? – glenn jackman Oct 9 '18 at 16:40
  • @glenn Jackman thank you sir, these number are just an example from my biology data which messed up. it is similar to the exact issue here i want to know how to fix it. i can't paste original data, it is against the protocol to share original data! this is example – user314642 Oct 9 '18 at 16:42
  • Can you explain the translation that you want? Aligned columns and a reversed last field? – Jeff Schaller Oct 9 '18 at 16:44
  • Your output still shows 969523 not 0.969523 in the first line. What should that value be? – glenn jackman Oct 9 '18 at 16:46
2
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
  • thank you this worked perfectly. I appreciate your time! – user314642 Oct 9 '18 at 17:07
2

Using awk, with a helper function to reverse the one field:

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

Save that to a file, then run: awk -f that-file-above < input > output.

On your sample input, it results in:

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
0
#!/bin/bash
var=$(cat file | column -t | cut -d ' ' -f 7 | rev)

var1=$(cat file | awk '{print $1, $2, $3}')

paste <(echo "$var1") <(echo "$var") --delimiters ' '

Note: this handles the data you had pasted into the text of the question which might be different than what you have in the original documents. Please adjust the number of the columns (in the commands awk and cut) according to your data.

  • thank you I will try I it when i go back home and let you knwo – user314642 Oct 9 '18 at 16:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy