按字段

时间:2017-02-19 01:43:55

标签: sorting unix awk

我想按降序排列线性回归(p)列的绝对值对此文件进行排序。我这样做的尝试并没有奏效。我不知道它失败了。我从http://www.unix.com/shell-programming-and-scripting/168144-sort-absolute-value.html找到了这段代码。

awk -F',' '{print ($2>=0)?$2:-$2, $0}' OFS=',' mycsv1.csv | sort -n -k8,8 | cut -d ',' -f2-



X var,Y var,MIC (strength),MIC-p^2 (nonlinearity),MAS (non-monotonicity),MEV (functionality),MCN (complexity),Linear regression (p)
AT1G01030,AT1G32310,0.67958,0.4832027,0.32644996,0.63247,4.0,-0.44314474
AT1G01030,AT3G06520,0.61732,0.17639545,0.23569,0.58557,4.0,0.6640215
AT1G01030,AT5G42580,0.61579,0.5019064,0.30105,0.58143,4.0,0.33746648
AT1G01030,AT1G55280,0.57287,0.20705527,0.19536,0.52857,4.0,0.6048262
AT1G01030,AT5G30490,0.56509,0.37536618,0.16172999,0.51847,4.0,-0.43557298
AT1G01030,AT1G80040,0.56268,0.22935495,0.18583998,0.52728,4.0,-0.5773431
...

请帮助我理解用于对此文件进行排序的awk脚本。

3 个答案:

答案 0 :(得分:2)

您可以使用sedsort来执行此操作,并按照@ hek2mgl非常智能的逻辑添加和删除末尾的字段以保留原始数字:

 sed -E 's/,([-]?)([0-9.]+)$/,\1\2,\2/' file | sort -t, -k9,9 -nr | cut -f1-8 -d,
  • sed -E 's/,([-]?)([0-9.]+)$/,\1\2,\2/' =>将字段9创建为字段8的绝对值
  • sort -t, -k9,9 -nr =>按新创建的字段,数字和降序排序
  • cut -f1-8 -d, =>删除第9个字段,使用所需的排序顺序将输出恢复为其原始格式

这是输出:

AT1G01030,AT3G06520,0.61732,0.17639545,0.23569,0.58557,4.0,0.6640215
AT1G01030,AT1G55280,0.57287,0.20705527,0.19536,0.52857,4.0,0.6048262
AT1G01030,AT1G80040,0.56268,0.22935495,0.18583998,0.52728,4.0,-0.5773431
AT1G01030,AT1G32310,0.67958,0.4832027,0.32644996,0.63247,4.0,-0.44314474
AT1G01030,AT5G30490,0.56509,0.37536618,0.16172999,0.51847,4.0,-0.43557298
AT1G01030,AT5G42580,0.61579,0.5019064,0.30105,0.58143,4.0,0.33746648

答案 1 :(得分:0)

采取三个步骤:

(1)暂时创建第9个字段,其中包含字段8的绝对值:

Vue.component('chat-message', require('./components/ChatMessage.vue'));
Vue.component('chat-room', require('./components/ChatRoom.vue'));
Vue.component('chat-composer', require('./components/ChatComposer.vue'));

const app = new Vue({
  el: '#app',
  data: {
    messages: []
  },
  methods: {
    addMessage(message) {
      this.messages.push(message);
      axios.post(base_url+'/room/1/write_message', message).then(response => { });
    }
  },
  created() {
    axios.get(base_url+'/room/1/messages').then(response => {
      this.messages = response.data;
      console.log(this.messages); //this returns an Array[4]!
    });
  }
});

(2)根据第9个字段对输出进行排序:

LC_COLLATE=C awk -F, 'NR>1{v=$NF;sub(/-/,"",v);printf "%s%s%s%s",$0,FS,v,RS}' file
        ^ ------ make sure this is set since sorting, especially the decimal point
             depends on the local.

(3)管道返回awk以删除最后一个字段。 command_1 | sort -t, -k9r 会减少有效删除最后一个字段的字段数。 NF--始终为真,这使1打印出该行:

awk

输出:

command_2 | cut -d, -f1-8

答案 2 :(得分:0)

可以让awk完成所有操作:

awk -F, 'NR>1{n[substr($NF,1,1)=="-"?substr($NF,2):$NF]=$0}NR==1;END{asorti(n,out);for(i in out)print n[out[i]]}' file