重击:找到第n列的最大值,并在其他列上加上条件

时间:2018-10-12 06:58:44

标签: awk

我有一个3列的沼泽文件,我想找到第三列的最大值,并在第一列相同的情况下写整行。我已经问过我的问题,但是现在的问题是,我的行数超过了最大行数。现在,我要选择第二列中具有最高价值的商品!

My previous question

我的数据:

    1 234 0.01
    1 235 0.05
    1 236 0.05
    1 237 0.02
    2 234 0.09
    2 235 0.09
    2 236 0.08
    2 237 0.05

输出:

    1 236 0.05
    2 235 0.09

2 个答案:

答案 0 :(得分:2)

解决方案第一: 。能否请您尝试以下操作。这将按照Input_file的顺序处理第一个字段的值。

awk '
FNR==NR{
  b[$1]=a[$1]>$3?b[$1]:$0
  a[$1]=a[$1]>$3?a[$1]:$3
  next
}
($1 in a){
  print b[$1]
  delete a[$1]
}
'   Input_file  Input_file

解决方案2: :如果您不在乎输出中$1(第一个字段)的顺序。

awk '{b[$1]=a[$1]>$3?b[$1]:$0;a[$1]=a[$1]>$3?a[$1]:$3} END{for(i in a){print b[i]}}' Input_file

答案 1 :(得分:0)

使用外壳工具:

class RentProduct{

public function init() {

    load_plugin_textdomain( 'eg', false, dirname( plugin_basename( __FILE__ ) ) . '/lang' );

    add_action( 'wp_enqueue_scripts', array( $this, 'register_plugin_styles' ) );

    // add_filter( 'the_content', array( $this, 'append_post_notification' ) );
}


public function register_plugin_styles() {
    wp_register_style( 'style', plugins_url( 'eg/assets/css/style.css' ) );
    wp_enqueue_style( 'style' );
}
}

add_action( 'init', array( 'RentProduct', 'init' ));

如果您的第一列可以匹配一定数量的字符,则它起作用。 $ sort -k3nr -k2nr file | uniq -w 5 2 235 0.09 1 236 0.05

man uniq

它适用于固定宽度的第一列,例如:

-w, --check-chars=N
       compare no more than N characters in lines

但不是:

1    Second column
1000 Second column